ruby-on-rails - Rails Responder 在创建时重定向到索引导致形成奇怪的行为

标签 ruby-on-rails

在我的 Controller 中,我有这个:

class TestController < ApplicationController
  respond_to :html, :json

  # code...

  def create
    @test = current_user.tests.create(params[:test])
    respond_with @test, location: tests_url
  end

  # code...

end

这很酷,当我创建 test 时,它重定向到 test#index (正如预期的那样),但是,如果我按 F5 ,浏览器要求我重新提交表单。

如果我删除 location声明来自 respond_with它工作得很好,但没有转到我想要的 URL。

我怎么能解决这个问题?

提前致谢。

编辑

我改变我的方法
@test = current_user.tests.new(params[:transaction])
respond_with(@test) do |format|
  format.html { redirect_to "/tests/" } if @test.save
end

它有效......但是,我不得不使用字符串而不是 tests_url 有点奇怪.

编辑 2

看到这个 complete example code
Bug report .

编辑 3

我无法用 Ruby 1.9.3-p327 重现它,只能在 -p385 中重现。

最佳答案

您可以使用 redirect_to方法,像这样:

def create
  @test = current_user.tests.create(params[:test])
  respond_with @test do |format|
    format.html { redirect_to tests_path }
  end
end

然后,如果客户端要求 json 响应,默认 to_json行为将被触发,但如果所需的响应格式是 html,则将发送重定向。

关于ruby-on-rails - Rails Responder 在创建时重定向到索引导致形成奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14836001/

相关文章:

ruby-on-rails - rake db :seed yields syntax error, unexpected '\n' , expecting => [closed]

ruby-on-rails - 尝试使用 React 在 Rails 应用程序上设置 ruby​​ 时出现 yarn 问题

javascript - 如何将 collection_select 中的值传递到 Rails 中的 onchange 函数?

ruby-on-rails - Rails sms_fu 错误

ruby-on-rails - rspec `with` 必须至少有一个参数。升级到 3.1.0 后报错

ruby-on-rails - AWS ELB 在重定向时继续在 Location header 中显示私有(private) IP

ruby-on-rails - 如何使用重新处理将已上传的回形针图像调整为 s3? ( rails )

ruby-on-rails - 在深度嵌套对象模型中处理 nils 的技术

ruby-on-rails - 在 ruby​​ on rails 中输出文本大小

ruby-on-rails - Rails 'link_to' 立即下载图像而不是在浏览器中打开它