Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding :user_id to a resource (rails3 routing)
    text
    copied!<p>I am trying to set up routes for the "Page" model nested within the "User" model. Only "Pages" needs to be resourcesful. (User has_many pages)</p> <p><strong>This is the original way I did it:</strong></p> <pre><code>resources :users, path: '', only: [] do resources :pages, path: '' end </code></pre> <p>The above worked for me just fine. The routes I got were these:</p> <pre><code> user_pages GET /:user_id(.:format) pages#index POST /:user_id(.:format) pages#create new_user_pages GET /:user_id/new(.:format) pages#new edit_user_pages GET /:user_id/:id/edit(.:format) pages#edit user_pages GET /:user_id/:id(.:format) pages#show PUT /:user_id/:id(.:format) pages#update DELETE /:user_id/:id(.:format) pages#destroy </code></pre> <p>This made sense for me because <code>/john-doe/page1</code> would be <code>user_pages_path(@user, @user.pages.first)</code>.</p> <p>However, new_user_pages didn't make sense because a user can only make a page for himself/herself. Therefore, each user should visit /new, not "/:user_id/new". Furthermore, what happens if the user visits another user's ":another_user_id/new" ? (it would make more sense to do new_pages_path and '/new' instead of <code>new_user_pages_path</code> and <code>/:user_id/new</code>).</p> <p><strong>Another way I tried to do the above routing:</strong></p> <p>I also realized the above can be accomplished in a shorter way due to the fact that ":users" does not need to be resourceful:</p> <pre><code>resources :pages, path =&gt; :user_id </code></pre> <p>However, this resulted in paths w/o "user" in them:</p> <pre><code> pages GET /:user_id(.:format) pages#index POST /:user_id(.:format) pages#create new_pages GET /:user_id/new(.:format) pages#new edit_pages GET /:user_id/:id/edit(.:format) pages#edit pages GET /:user_id/:id(.:format) pages#show PUT /:user_id/:id(.:format) pages#update DELETE /:user_id/:id(.:format) pages#destroy </code></pre> <hr> <p>What is the "rails" way of doing this? Also, should I remove "new" from the resource and define it separately?</p> <p>Also, does it make sense to use <strong>scope</strong> or <strong>namespace</strong> instead?</p> <p>Thanks, Nick</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