在Rails 5中更改命名路由的名称

时间:2016-11-12 10:50:41

标签: ruby-on-rails ruby

我有以下routes.rb

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

我希望命名路由为read_authors_books,但它生成为authors_books_read,有没有办法更改命名路由?

我已尝试post "read" as: :read_authors_books,但会将其生成为authors_books_read_authors_books

1 个答案:

答案 0 :(得分:0)

:read设为member_route

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

这将生成

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