Noturab Noturab - 28 days ago 8
Ruby Question

Change name of the named route in Rails 5

I have a following

routes.rb


resources :authors do
resources :books, only: [:create, :read] do
post "read"
end
end


I would like to have the named route to be
read_authors_books
, but it gets generated as
authors_books_read
, is there a way to change the named route?

I have tried
post "read" as: :read_authors_books
but it generates it as
authors_books_read_authors_books

Answer

Create :read as a member_route

resources :authors do
  resources :books, only: [:create] do
    post :read, on: :member
  end
end

This will generate

read_author_book POST   /authors/:author_id/books/:id/read(.:format) books#read
    author_books POST   /authors/:author_id/books(.:format)          books#create

Read more about member route here