Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 Routing Resources with Variable Namespace
    text
    copied!<p>Is it possible to have a variable namespace? I have restful resources like the following:</p> <pre><code>resources :articles resources :persons </code></pre> <p>But I need to scope these inside a variable namespace, such that it responds to URLs of the form:</p> <pre><code>':edition/:controller/:action/:id' </code></pre> <p>for example:</p> <p><code>/foobar/article/edit/123</code> or <code>/bazbam/person/edit/345</code></p> <p>for each of the resources. Is this possible with the <code>resources</code> method, or must I hand-craft these? I will not know the possible values for <code>:edition</code> ahead of time; these get looked up in a <code>before_filter</code> in my <code>ApplicationController</code>.</p> <p>Is this all I need to do?</p> <pre><code>scope ':edition' do resources :articles resources :matches resources :teams end </code></pre> <p><strong>UPDATE: When using the scope directive above, I get routes like I want:</strong></p> <pre><code> articles GET /:edition/articles(.:format) {:action=&gt;"index", :controller=&gt;"articles"} POST /:edition/articles(.:format) {:action=&gt;"create", :controller=&gt;"articles"} new_article GET /:edition/articles/new(.:format) {:action=&gt;"new", :controller=&gt;"articles"} edit_article GET /:edition/articles/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"articles"} article GET /:edition/articles/:id(.:format) {:action=&gt;"show", :controller=&gt;"articles"} PUT /:edition/articles/:id(.:format) {:action=&gt;"update", :controller=&gt;"articles"} DELETE /:edition/articles/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"articles"} matches GET /:edition/matches(.:format) {:action=&gt;"index", :controller=&gt;"matches"} POST /:edition/matches(.:format) {:action=&gt;"create", :controller=&gt;"matches"} new_match GET /:edition/matches/new(.:format) {:action=&gt;"new", :controller=&gt;"matches"} edit_match GET /:edition/matches/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"matches"} match GET /:edition/matches/:id(.:format) {:action=&gt;"show", :controller=&gt;"matches"} PUT /:edition/matches/:id(.:format) {:action=&gt;"update", :controller=&gt;"matches"} DELETE /:edition/matches/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"matches"} teams GET /:edition/teams(.:format) {:action=&gt;"index", :controller=&gt;"teams"} POST /:edition/teams(.:format) {:action=&gt;"create", :controller=&gt;"teams"} new_team GET /:edition/teams/new(.:format) {:action=&gt;"new", :controller=&gt;"teams"} edit_team GET /:edition/teams/:id/edit(.:format) {:action=&gt;"edit", :controller=&gt;"teams"} team GET /:edition/teams/:id(.:format) {:action=&gt;"show", :controller=&gt;"teams"} PUT /:edition/teams/:id(.:format) {:action=&gt;"update", :controller=&gt;"teams"} DELETE /:edition/teams/:id(.:format) {:action=&gt;"destroy", :controller=&gt;"teams"} </code></pre> <p>I'm now able to reference <code>:edition</code> in my <code>ApplicationController</code>:</p> <pre><code>class ApplicationController &lt; ActionController::Base protect_from_forgery before_filter :authenticate_user! before_filter :get_edition def get_edition @edition = Edition.first(:conditions =&gt; { :FriendlyName =&gt; params[:edition] } ) end end </code></pre> <p>Now I just want to make sure this is the best way to accomplish this.</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