So often I have a form in some webpage that the user submits to a POST, PUT or DELETE action in Rails where I want it to redirect to a specified URL if the submission was a success. I typically make a hidden extra parameter called to
with a path like /users
. So if the form submission failed, it just stays on that form, but if it succeeds then the browser is redirected to /users
.
I'd like to automatically look for this parameter and always redirect to it if a form submission succeeded in any controller/action. Do I put this in the ApplicationController
within an after_action
?
class ApplicationController < ActionController::Base
after_action :redirect_if_success
private
def redirect_if_success
redirect_to params[:to] if params[:to]
end
end
I guess I can check the request object if this was a POST, PUT or DELETE action. How do I know the submission was a success? Will a redirect_to
in the after_action
override any redirect_to
s in the form controller?