Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To answer your comment on another answer:</p> <blockquote> <p>I'm wondering if there's a way, without rewriting the controller at all, to tell rails that you want the URL to match the rendered template, rather than the controller action that called it.</p> </blockquote> <p>I don't think so; URLs are tied directly to routing, which is tied into a controller and action pair--the rendering layer doesn't touch it at all.</p> <p>To answer <strong>your original question</strong>, here's information from <a href="https://stackoverflow.com/questions/5047136/the-correct-way-to-handle-invalid-form-submissions-in-rails/5054434#5054434">another similar question</a> I answered.</p> <hr> <p>As you've found, by default when you specify <code>resources :things</code>, the POST path for creating a new thing is at <code>/things</code>. Here's the output for <code>rake routes</code>:</p> <pre><code> things GET /things(.:format) {:action=&gt;"index", :controller=&gt;"things"} POST /things(.:format) {:action=&gt;"create", :controller=&gt;"things"} new_thing GET /things/new(.:format) {:action=&gt;"new", :controller=&gt;"things"} edit_thing GET /things/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"things"} thing GET /things/:id(.:format) {:action=&gt;"show", :controller=&gt;"things"} PUT /things/:id(.:format) {:action=&gt;"update", :controller=&gt;"things"} DELETE /things/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"things"} </code></pre> <p>It sounds like you want something more like this:</p> <pre><code>create_things POST /things/new(.:format) {:action=&gt;"create", :controller=&gt;"things"} things GET /things(.:format) {:action=&gt;"index", :controller=&gt;"things"} new_thing GET /things/new(.:format) {:action=&gt;"new", :controller=&gt;"things"} edit_thing GET /things/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"things"} thing GET /things/:id(.:format) {:action=&gt;"show", :controller=&gt;"things"} PUT /things/:id(.:format) {:action=&gt;"update", :controller=&gt;"things"} DELETE /things/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"things"} </code></pre> <p>Although not recommended, you can get this result with the following route:</p> <pre><code>resources :things, :except =&gt; [ :create ] do post "create" =&gt; "things#create", :as =&gt; :create, :path =&gt; 'new', :on =&gt; :collection end </code></pre> <p>You would also need to modify your forms to make them POST to the correct path.</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