Redirecting to GET after PUT in Rails (status: 303) does not work

In Rails 5.1, I am doing PUT and trying to redirect with a specific error:

rescue_from ActionController::InvalidAuthenticityToken do redirect_to new_user_session_url, status: 303 and return end 

The docs show that status: 303 should make redirect_to called as GET, but it is still PUT.

How can I do this redirect as GET?

thanks

+5
source share
3 answers

If you do this using Ajax, your request is processed as JS, then your redirect should not work, because you need to get the answer in your full Ajax call.

Finally, you should redirect within the full callback sending the url with your controller.

I hope this helps you

+1
source

I could do a PUT relay in browser and in curl

 # Gemfiile gem 'rails' gem 'pry' gem 'puma' # app.rb require 'rails' require 'action_controller' require 'rack/handler/puma' class InvalidAuthenticityToken < StandardError end # HelloController class HelloController < ActionController::Base def index render inline: ' <form action="/" method="POST"> <input type="hidden" name="_method" value="put" /> First name:<br> <input type="text" name="firstname" value="Mickey"> <br> Last name:<br> <input type="text" name="lastname" value="Mouse"> <br><br> <input type="submit" value="Submit"> </form> ' end def update raise InvalidAuthenticityToken end def redirect render plain: 'you are redirected' end rescue_from InvalidAuthenticityToken do redirect_to '/you_are_redirected' end end class MyApp < Rails::Application end app = MyApp.new app.config.secret_key_base = 'my-secret' app.initialize! app.routes.draw do get '/' => 'hello#index' get '/you_are_redirected' => 'hello#redirect' put '/' => 'hello#update' put '/you_are_redirected' => 'hello#redirect' post '/you_are_redirected' => 'hello#redirect' # for browser request end Rack::Handler::Puma.run app 

Run it

 bundle bundle exec ruby app.rb 

visit localhost:9292 or

 curl -X PUT localhost:9292 -L you are redirected% 

I want this to help

+1
source

You requested no http, so the status will not work. Change the redirection to the following: render :js => "window.location = '/users/signup'" or any of your URLs new_user_session_path`.

+1
source

Source: https://habr.com/ru/post/1272675/


All Articles