darksky
darksky

Reputation: 21039

Adding Rails Individual Route

I have a CRUD resource defined in my routes.rb file: resource :user.

I'm adding a new controller method for the user called search_places, which is performed on the user to find other users with the same places. I'm adding a route it.

Right now, I have:

post '/user/search_place', which isn't very DRY. I'm new to Rails and I was reading the Rails routing documentation and figured that I could possibly use

resource :user do
  collection do
    post 'search_place'
  end
end

Is this considered good practice? I know this works (it passes my rspec route test), but is that how its best done?

Thank you,

Upvotes: 2

Views: 56

Answers (1)

Rahul Tapali
Rahul Tapali

Reputation: 10137

When you add second don't need of first.

Add this:

 resources :user do
   collection do
     post 'search_place'
   end
 end

Remove this:

 resources :user

That makes DRY :)

Suggestion: Resources name should be defined in plural if u follow rails convention. (i.e) resources :users

Upvotes: 1

Related Questions