我正在向控制器动作发送一个javascript请求,如果没有满足某些条件,就会尝试重定向到另一个html页面。在查看了一些问题后,这是我想出的方法:
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
我想要重定向到认证URL(authentications_url)。然而,下面是发生的事情:
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)
正如您所看到的,它甚至没有尝试重定向到验证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"
):
format.js { redirect_to authentications_url }
- DanS