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
That’s cool, when I create the test, it redirects to test#index (as expected), but, if I press F5, the browser asks me to resubmit the form.
If I remove the location statement from respond_with it works just fine, but doesn’t go to URL I want.
How could 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’s kinda weird I had to use a String instead of tests_url.
EDIT 2
See this complete example code amd the
Bug report.
EDIT 3
I’m unable to reproduce it with Ruby 1.9.3-p327, only in -p385.
You could just use the
redirect_tomethod, like this:Then, if the client asks for a json response, the default
to_jsonbehavior will be triggered, but if the response format desired is html, then a redirect will be sent.