JavaScript请求重定向到HTML页面不起作用(Rails)

5

我正在向控制器动作发送一个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"
):    

您需要使用render(:update)吗?format.js { redirect_to authentications_url } - DanS
@DanS 如果我这样做,它会显示“Started GET”/authentications“for 127.0.0.1 at 2012-04-11 13:57:03 -0400 Processing by AuthenticationsController#index as */*”等等...但实际上并没有加载页面。 - varatis
@DanS 换句话说,它显示已呈现“authentications/index.html.erb”,但实际上并没有执行任何操作。 - varatis
1个回答

12

我猜你是通过ajax调用了twittertweet,然后通过javascript代码对其进行响应:

format.js { render :js => "window.location.href = '#{some_path}'" }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接