Note that there are some explanatory texts on larger screens.

plurals
  1. POCan a custom path be appended to the 'new' path for Rails routes?
    text
    copied!<p><strong>What I'm trying to do:</strong></p> <p>I'm building a system where there are different types of post. Setting aside the models, this question is about the routes and the controller</p> <p>Basically <code>/posts/new</code> should go to an index page of sorts, while <code>/posts/new/anything</code> should look up the type <code>anything</code> and then build a form for creating a new one.</p> <p><strong>How I'm trying to do it:</strong></p> <p>Feel free to ignore this section as I could be completely on the wrong track.</p> <p>In the routes config:</p> <pre><code>map.connect '/posts/new', :controller =&gt; 'posts', :action =&gt; 'new_index' map.resources :posts, :path_names =&gt; { :new =&gt; 'new/:type' } </code></pre> <p>In the controller:</p> <pre><code>class PostsController # implicit: def new_index ; end def new @post = class_for_type(params[:type]).new end end </code></pre> <p>The view has code which looks at the type of @post to determine which set of views to use. Turns out this gets me 90% of the way there: <code>/posts/new/quip</code> does actually send me to the correct page to create a quip, and so forth. <code>/posts/new</code> does send me to an index page.</p> <p>Problem is twofold.</p> <ol> <li><p>I still want to have convenience methods like this:</p> <pre><code>&lt;%= link_to 'New Post', new_post_path %&gt; </code></pre> <p>But this is now invalid as <code>new_post_path</code> requires the <code>:type</code> parameter.</p></li> <li><p>I would like to do it using one route if possible.</p></li> </ol>
 

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