如何使资源重定向到Rails中的另一个控制器 [英] How to make resources redirect to another controller in rails

查看:47
本文介绍了如何使资源重定向到Rails中的另一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two models: Book and Magazine. There are few differences in terms of attributes, but i want them to share the same controller and views (the ones of Book's model).

我的问题是:考虑到Book已按照以下 resources:books

My question is : What is the correct way to set the routes of Magazine model inside routes.rb, considering that Book is already set as following resources :books

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

This is a basic question, but i want to learn the best way instead of defining all the routes manually one by one

谢谢!

推荐答案

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

You can configure the resource routes to point to a specific 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

这篇关于如何使资源重定向到Rails中的另一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆