Rails Responder Redirects to Index in Creation Causes Strange Behavior

In my controller, I have this:

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 

This is great when I create test , it redirects to test#index (as expected), but if I press F5 , the browser will ask me to resubmit the form.

If I remove the location statement from respond_with , it works fine, but does not go to the url I want.

How can i fix this?

Thanks in advance.


EDIT

I change my method to

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

And it works .. but, it was strange, I had to use String instead of tests_url .


EDIT 2

See full sample code and bug report .


EDIT 3

I cannot play it with Ruby 1.9.3-p327, only at -p385.

+4
source share
1 answer

You can simply use the redirect_to method, for example:

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

Then, if the client requests a json response, the default to_json behavior will fire, but if the desired response format is html, a redirect will be sent.

0
source

All Articles