Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails routes: GET without param :id

I'm developing a REST API based on rails. To use this API, you MUST be logged in. Regarding that, I'd like to create a method me in my user controller that will return a JSON of the logged in user infos. So, I don't need an :id to be passed in the URL. I just want to call http://example.com/api/users/me

So I tried this:

namespace :api, defaults: { format: 'json' } do
  scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
    resources :tokens, :only => [:create, :destroy]
    resources :users, :only => [:index, :update] do

      # I tried this
      match 'me', :via => :get
      # => api_user_me GET    /api/users/:user_id/me(.:format)       api/v1/users#me {:format=>"json"}

      # Then I tried this
      member do
        get 'me'
      end
      # => me_api_user GET    /api/users/:id/me(.:format)            api/v1/users#me {:format=>"json"}

    end
  end
end

As you can see, my route waits for an id, but I'd like to get something like devise has. Something based on current_user id. Example below:

edit_user_password GET    /users/password/edit(.:format)         devise/passwords#edit

In this example you can edit the current user password without passing the id as a param.

I could use a collection instead of a member, but that's a dirty bypass.

like image 569
Gozup Avatar asked Jun 12 '13 13:06

Gozup


People also ask

How do I find routes in Rails?

In Rails, the routes of your application live in config/routes. rb . The Rails router recognises URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views.

What is rake routes?

rake routes will list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you're trying to get familiar with.

What are RESTful routes in Rails?

In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as. map.resources :photos. creates seven different routes in your application: HTTP verb.

What is the difference between resources and resource in Rails?

Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.


3 Answers

The way to go is to use singular resources:

So, instead of resources use resource:

Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action [...]

So, in your case:

resource :user do
  get :me, on: :member
end

# => me_api_user GET    /api/users/me(.:format)            api/v1/users#me {:format=>"json"}
like image 83
Waiting for Dev... Avatar answered Nov 01 '22 14:11

Waiting for Dev...


Resource routes are designed to work this way. If you want something different, design it yourself, like this.

match 'users/me' => 'users#me', :via => :get

Put it outside of your resources :users block

like image 24
Arjan Avatar answered Nov 01 '22 15:11

Arjan


You can use

resources :users, only: [:index, :update] do
  get :me, on: :collection
end

or

resources :users, only: [:index, :update] do
  collection do
    get :me
  end
end

"A member route will require an ID, because it acts on a member. A collection route doesn't because it acts on a collection of objects. Preview is an example of a member route, because it acts on (and displays) a single object. Search is an example of a collection route, because it acts on (and displays) a collection of objects." (from here)

like image 20
imtk Avatar answered Nov 01 '22 15:11

imtk