1  module Enumerable
   2    def take_while
   3      inject([]) do |xs, x|
   4        yield x or break xs
   5        xs << x
   6      end
   7    end
   8  end

posted by Png faerie on Tue 12 Aug 2008 at 20:34

begin~rescue~endで処理する。 例外のメッセージにパターンがある場合はex.messageが特定文言を含むかどうか調べて人間に優しいメッセージに変更する。

   1    def destroy
   2      @article = Article.find(params[:id])
   3      begin
   4        if @article.destroy
   5          flash[:notice] = '記事を削除しました。'
   6        end
   7      rescue => ex
   8        #「violates foreign key」は外部キーの参照エラー。関連テーブルが存在するため削除できないことを意味する。
   9        if ex.message=~/violates foreign key/
  10          flash[:notice] = '該当記事を参照しているデータがあるため削除できません'
  11        else
  12          #その他の例外
  13          flash[:notice] = ex.message
  14        end
  15      end
  16      redirect_to :action => 'index'
  17    end

posted by Face ysakaki on Tue 12 Aug 2008 at 12:01
Contents
Enumerable#take_while
Rails(Ruby)で例外処理
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ