如何使用单表继承 (STI) 为 Ruby on Rails 中的继承资源生成适当的路由助手
我有两个资源:FaxRequest 和 InternetRequest,它们都继承自 Request。我使用rails脚手架资源命令生成了这三个资源中的每一个,然后修改模型以从Request继承,如下所示:
#request.rb
class Request < ActiveRecord::Base
end
#fax_request.rb
class FaxRequest < Request
end
#internet_request.rb
class InternetRequest < Request
end
我想要一个视图,其中包含一个显示所有请求(FaxRequests和InternetRequests)一起。我将其实现为请求控制器的索引视图。
#request_controller.rb
def index
@requests = Request.all
end
#views/requests/index.html.haml
[email protected] do |request|
= request.date
= link_to('show', request)
= link_to('edit', ?)
= link_to('delete' ?)
我发现 link_to('view', request)
足够智能,可以生成指向相应资源的链接,但是 Rails 如何知道是否生成 edit_fax_request_path(request)< /code> 或
edit_internet_request_path(request)
基于请求的子类?新建和删除也一样吗?
其次,我很想知道性传播感染的惯用方法是什么。在这种情况下是一个控制器还是三个控制器?我对这个问题的理解是不是全错了?在我看来,Rails 有一个简洁的方法来处理这个问题,但我用谷歌搜索并找不到任何东西。也许我没有问正确的问题?
I have two resources: FaxRequest and InternetRequest that both inherit from Request. I generated each of the three resources using the rails scaffold resource
command and then modified the models to inherit from Request like this:
#request.rb
class Request < ActiveRecord::Base
end
#fax_request.rb
class FaxRequest < Request
end
#internet_request.rb
class InternetRequest < Request
end
I would like to have a view that has a table showing all Requests (both FaxRequests and InternetRequests) together. I have this implemented as the index view of the Request controller.
#request_controller.rb
def index
@requests = Request.all
end
#views/requests/index.html.haml
[email protected] do |request|
= request.date
= link_to('show', request)
= link_to('edit', ?)
= link_to('delete' ?)
I see that link_to('view', request)
is smart enough to generate a link to the appropriate resource, but how does Rails know whether to generate an edit_fax_request_path(request)
or edit_internet_request_path(request)
based on the subclass of the request? Same goes for new and delete?
Secondarily, I'm curious to know what the idomatic approach to STI is. One controller or three in this case? Am I going about the problem all wrong. It seems to me that Rails would have a tidy way of dealing with this problem, yet I've googled and can't find anything. Perhaps I'm not asking the right question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所需要做的就是使用
edit_request_path(request)
辅助方法来编辑链接。至于删除链接,这里是脚手架将生成的删除链接代码,一旦您按照我在下面概述的操作进行操作,就会起作用:然后编辑您的路线文件,告诉rails生成适当的路线来发送
请求<像这样的 /code> 控制器(注意,您可能不需要
map
部分):这样,所有路由都会按照您的预期生成,并且它们都会转到它们继承的主控制器。
更新:找到一篇很棒的文章讨论这个(并警告我所说的一些内容)
http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html
http://stackoverflow.com/questions/4507149/best-practices-to-handle-routes-for-sti-subclasses-in-rails
All you need to do is to use the
edit_request_path(request)
helper method for the edit link. As for the delete link, here is the delete link code that scaffolding would of generated and will work, once you do as I outline below:Then edit your routes file telling rails to generate the appropriate routes to go to the
request
controller like so (note, you might not need themap
part):This way all the routes are generated as you would expect and they all go to the main controller that they inherited from.
Update: Found a great article talking about this (and warns about some of what I say)
http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html
http://stackoverflow.com/questions/4507149/best-practices-to-handle-routes-for-sti-subclasses-in-rails