My controller is used for both HTML and JSON requests, and behaves differently for each. Normally with the JSON request it will just render a JSON response, but if a special parameter is sent from the client, the JSON handler should perform a redirect as well:
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
return unless current_user == @post.user
respond_to do |format|
if @post.update(post_params)
format.html {
puts "HTML"
redirect_to @post, notice: 'Post was successfully updated.'
}
format.json {
puts "JS"
if params[:redirect].to_s == "true"
puts "REDIRECT"
# ~~~~ THIS REDIRECT ISNT WORKING ~~~~
redirect_to @post, notice: 'Post was successfully updated.'
else
puts "NO REDIRECT"
render json: @post
end
}
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
Through those puts statements I can verify that the request is coming in with format.json and params[:redirect].to_s == "true".
But I can see something peculiar in the Rails console logs:
Started PUT "/posts/2" for ::1 at 2020-07-08 01:43:06 -0700 Processing by PostsController#update as JSON ... other log output ... JS REDIRECT Redirected to http://localhost:3000/posts/2 Completed 200 OK in 33ms (ActiveRecord: 13.4ms | Allocations: 18286)
Why is it using status code 200 after a redirect? And in the browser I can confirm that the response has code 200.
The browser is not redirecting after making the AJAX submission. And I suspect it has to do with this incorrect status code.
I tried explicitly passing status: 301 in the redirect_to call but it made no difference.
Update after writing out this question and thinking about it a little, I suppose it's probably easier to just manually do the redirect using the success handler the browser. But I'm still curious why this doesn't work.