Bohn
Bohn

Reputation: 26919

Upgrading routes in Rails 3

Currently I have something like this:

  resources :books do
    collection do
      get 'search'
    end
  end

and my controller name is also "books" and I have a action method called "search" inside it

I would like that "get search" part to also be a resource, kind of like nested resources... but I don't want to break other peoples codes that are using the current route that this generate, so need to update it in a passive way!

Upvotes: 1

Views: 45

Answers (2)

sjain
sjain

Reputation: 23344

Use shallow routes nesting like:

resources :books , :shallow => true do
  resources :searches
end

Now you will get the following routes:

/books/1 => books_path(1)
/books/1/searches => books_searches_index_path(1)
/searches/2 => searches_path(2)

Similarly you can get separate routing for defined routes like:

get '(:books)/searches', :to => 'books#index'

Upvotes: 0

varatis
varatis

Reputation: 14740

resources :books do
  collection do
    get 'search'
  end
  resources :searches
end

...if I'm understanding you correctly, that should be what you want. It won't break other routes, just add new ones.


Run rake routes to make sure you have all the routes you want/need.

Upvotes: 1

Related Questions