2010-12-11 31 views
1

どういう意味ですか? redirect_to ""と返すRuby on rails redirect_to

+0

参照このイディオムがブール 'and'の短絡評価を利用することもhttp://guides.rubyonrails.org/layouts_and_rendering.html#using-redirect_to – Zabba

答えて

5

rails APIドキュメントによると、return部分は他のものの実行を停止します。言い換えれば、もしあなたが以下のものを持っていれば、return文のためにテキストは決して印刷されません。次の例and return

def go_home 
    redirect_to(:action => "home") and return 
    puts "This will never print" 
    end 

のみif monkeys.nil?が真であると呼ばれます。

def do_something 
    redirect_to(:action => "elsewhere") and return if monkeys.nil? 
    render :action => "overthere" # won't be called if monkeys is nil 
    end 

から:http://api.rubyonrails.org/classes/ActionController/Base.html

+0

注表現。 'redirect_to'が' false'を返した場合、Rubyは短絡をとり、残りの式を評価することはありません。しかし、 'redirect_to'が' true'を返す場合、残りの式を評価する必要があります。残りが 'return'であるので、式全体の効果は:"リダイレクト(302)コードを発行し、これが本当に起こったら、他のアクションをとることなくリターン "です。 –