Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: URL after validation fails when creating new records via form
    text
    copied!<p>Lets say I am creating a new Foo using a form and a standard Rails restful controller, which looks something like this:</p> <pre><code>class FoosController &lt; ApplicationController ... def index @foos = Foo.all end def new @foo = Foo.new end def create @foo = Foo.create(params[:foo]) if @foo.save redirect_to foos_path, :notice =&gt; 'Created a foo.' else render 'new' end end ... end </code></pre> <p>So, if I use the standard restful controller (as above), then when I'm creating the Foo I am at <code>example.com/foos/new</code>, and if I submit the form and it saves correctly I'm at <code>example.com/foos</code> showing the index action. However, if the form is not filled correctly the form is rendered again and error messages are shown. This is all plain vanilla.</p> <p>However, if errors are shown, the form page will be rendered but the URL will be <code>example.com/foos</code>, because the CREATE action posts to that url. However, one would expect to find Foos#index at <code>example.com/foos</code>, not the form they just submitted now with error messages added.</p> <p>This seems to be Rails standard behavior, but it doesn't make a lot of sense to me. Obviously I could redirect back to new instead of rendering new from the create action, but the problem with that is the error messages etc. would be lost along with the partially complete Foos in memory.</p> <p>Is there a clean solution for this problem, a way to send people back to <code>example.com/foos/new</code> when there are errors in the new Foo form they submitted?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload