Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What about leaving the routes the way they are in your post, and just solving this inside the controller?</p> <p>Heres a <code>before_filter</code> that you could apply to all of the routes you have which pull a <code>User</code> from a <code>:user_id</code>.</p> <pre><code># Set the @user variable from the current url; # Either by looking up params[:user_id] or # by assigning current_user if params[:user_id] = 'me' def user_from_user_id if params[:user_id] == 'me' &amp;&amp; current_user @user = current_user else @user = User.find_by_user_id params[:user_id] end raise ActiveRecord::RecordNotFound unless @user end </code></pre> <p>Then in your controller functions you can just use the <code>@user</code> variable without having to worry about whether the user passed a <code>user_id</code>, or <code>me</code>. </p> <p>Hope that helps! :)</p> <p>EDIT:</p> <p>Lemme take another shot, given your comments.</p> <p>How about a function that lists all the resources you wish to access via both the standard routes and the <code>/me</code> route. Then you can just use the function in both the namespaces you require.</p> <h3>routes.rb</h3> <pre><code># Resources for users, and for "/me/resource" def user_resources resources :posts resources :likes ... end namespace 'api' do namespace 'v1' do resources :users do user_resources end end end scope '/api/v1/:user_id', :constraints =&gt; { :user_id =&gt; 'me' }, :defaults =&gt; {:format =&gt; 'json'}, :as =&gt; 'me' do user_resources end # We're still missing the plain "/me" route, for getting # and updating, so hand code those in match '/api/v1/:id' =&gt; 'users#show', :via =&gt; :get, :constraints =&gt; { :id =&gt; 'me' } match '/api/v1/:id' =&gt; 'users#update', :via =&gt; :put, :constraints =&gt; { :id =&gt; 'me' } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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