【问题标题】:Redirect to html from javascript request not working (Rails)从 javascript 请求重定向到 html 不起作用(Rails)
【发布时间】:2012-04-24 01:38:02
【问题描述】:

如果不满足某些条件,我正在向控制器操作发送 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

我想重定向_到 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)

如您所见,它甚至没有尝试重定向_到 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"
):    

【问题讨论】:

  • 你需要渲染(:update)吗? format.js { redirect_to authentications_url }
  • @DanS 如果我这样做,它会在 2012-04-11 13:57:03 -0400 处理 AuthenticationsController#index as / ”等...但实际上并没有加载页面
  • @DanS 换句话说,它说它已经渲染了“authentications/index.html.erb”,但实际上并没有做任何事情

标签: ruby-on-rails ruby-on-rails-3 redirect ruby-on-rails-3.1


【解决方案1】:

我猜你通过 ajax 调用 twittertweet。然后通过javasctipt代码响应:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 2011-01-07
    • 2014-01-19
    相关资源
    最近更新 更多