我有两个模型:Book
和 Magazine
。在属性方面几乎没有区别,但我希望它们共享相同的 Controller 和 View (Book 的模型)。
我的问题是:在 routes.rb 中设置 Magazine 模型路由的正确方法是什么,考虑到 Book 已经设置如下 resources :books
这是一个基本问题,但我想学习最好的方法,而不是一个一个地手动定义所有路由
谢谢!
最佳答案
您可以将资源路由配置为指向特定的 Controller :
resources :books
resources :magazines, controller: 'books'
这将创建以下路由:
books GET /books(.:format) books#index
POST /books(.:format) books#create
new_book GET /books/new(.:format) books#new
edit_book GET /books/:id/edit(.:format) books#edit
book GET /books/:id(.:format) books#show
PATCH /books/:id(.:format) books#update
PUT /books/:id(.:format) books#update
DELETE /books/:id(.:format) books#destroy
magazines GET /magazines(.:format) books#index
POST /magazines(.:format) books#create
new_magazine GET /magazines/new(.:format) books#new
edit_magazine GET /magazines/:id/edit(.:format) books#edit
magazine GET /magazines/:id(.:format) books#show
PATCH /magazines/:id(.:format) books#update
PUT /magazines/:id(.:format) books#update
DELETE /magazines/:id(.:format) books#destroy
关于ruby-on-rails - 如何使资源重定向到rails中的另一个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25293107/