特定の条件が満たされない場合に別の HTML ページにリダイレクトしようとするコントローラー アクションに JavaScript 要求を送信しています。いくつかの質問に目を通した後、これが私が思いついたものです:
def twittertweet
if current_user && current_user.twitter_token
....
else
flash[:notice] = "You must be registered with Twitter to do that."
respond_to do |format|
format.js { render(:update) { |page| page.redirect_to authentications_url } }
end
end
end
authentications_url に redirect_to したい。ただし、次のようになります。
Started POST "/twittertweet.json" for 127.0.0.1 at 2012-04-11 13:38:27 -0400
Processing by ItemsController#twittertweet as */*
Parameters: {"id"=>"22"}
Category Load (0.3ms) SELECT "categories".* FROM "categories"
Rendered items/update.html.erb within layouts/application (0.0ms)
Completed 200 OK in 112ms (Views: 79.9ms | ActiveRecord: 5.7ms)
ご覧のとおり、authentications_url にリダイレクトしようとさえしていません。それでも、「else」ステートメントに到達していることはわかっています (それを見つけるのに役立つ puts ステートメントを入れました)。
これに対する回避策はありますか?
更新 DanS のソリューションを試すと、MissingTemplateError が発生します。それはまだ items#update にリダイレクトしようとしています:
ActionView::MissingTemplate (Missing template items/update, application/update with {:handlers=>[:erb, :builder, :coffee], :formats=>[:json], :locale=>[:en, :en]}. Searched in:
* "/Users/dylandrop/Documents/givespend/app/views"
* "/Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/devise-2.0.4/app/views"
):