如何使资源重定向到rails中的另一个控制器

use*_*206 3 redirect routes ruby-on-rails ruby-on-rails-3

我有两个模型:BookMagazine。属性方面几乎没有差异,但我希望它们共享相同的控制器和视图(Book 模型的控制器和视图)。

我的问题是:考虑到 Book 已经设置如下,在 paths.rb 中设置 Magazine 模型的路线的正确方法是什么resources :books

这是一个基本问题,但我想学习最好的方法,而不是一一手动定义所有路由

谢谢!

inf*_*sed 5

您可以将资源路由配置为指向特定控制器:

resources :books
resources :magazines, controller: 'books'
Run Code Online (Sandbox Code Playgroud)

这将创建以下路由:

                             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
Run Code Online (Sandbox Code Playgroud)