title: Redirect to a specified URL after a POST in Rails
body: <p>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 <code>to</code> with a path like <code>/users</code>. So if the form submission failed, it just stays on that form, but if it succeeds then the browser is redirected to <code>/users</code>.</p>
  
  <p>I'd like to automatically look for this parameter and always redirect to it if a form submission succeeded in <strong><em>any</em></strong> controller/action. Do I put this in the <code>ApplicationController</code> within an <code>after_action</code>?</p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
    after_action :redirect_if_success
  
    private
    def redirect_if_success
      redirect_to params[:to] if params[:to]
    end
  end
  </code></pre>
  
  <p>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 <code>redirect_to</code> in the <code>after_action</code> override any <code>redirect_to</code>s in the form controller?</p>