Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I change the default parameter names in Rails 3 routing?
    text
    copied!<p>In my Rails 3 project I have a list of routes like this:</p> <pre><code>resources :projects do resources :studies end resources :sticky_notes resources :study_templates ... </code></pre> <p>Currently by default the ids in the URLs from these routes can be called with params[:id], but I want to be able to call them with params[:sticky_note_id], params[:study_template_id], params[:study_id], etc. Is there a way I can specify the parameter name for the ID of these projects? Do I have to write each route out manually without 'resources'?</p> <p>Thanks!</p> <p>Edit: Here's an example of what i'm trying to do: This is what happens when the routes are defined as written above:</p> <pre><code>resources :projects do resources :studies end # results in /projects/:project_id/studies/:id # /projects/:project_id/studies/:id/edit # /projects/:project_id/studies/:id/new # etc. resources :sticky_notes # results in /sticky_notes/:id # /sticky_notes/:id/edit # /sticky_notes/:id/new # etc. </code></pre> <p>This is what I want:</p> <pre><code>match '/projects/:project_id/studies/:study_id' =&gt; 'studies#show' match '/projects/:project_id/studies/:study_id/edit' =&gt; 'studies#edit' match '/projects/:project_id/studies/:study_id/new' =&gt; 'studies#new' ... # results in /projects/:project_id/studies/:study_id # /projects/:project_id/studies/:study_id/edit # etc match '/sticky_notes/:sticky_note_id' =&gt; 'sticky_notes#show' match '/sticky_notes/:sticky_note_id/edit' =&gt; 'sticky_notes#edit' match '/sticky_notes/:sticky_note_id/new' =&gt; 'sticky_notes#new' ... # results in /sticky_notes/:sticky_note_id # /sticky_notes/:sticky_note_id/edit # etc </code></pre> <p>I want the second part, but without all that work on my already-huge routes file. :) is it possible?</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