简体   繁体   English

Rails 多态关系和link_to

[英]Rails Polymorphic relationship and link_to

Here's my Schema这是我的架构

class Menu < ActiveRecord::Base
  belongs_to :menuable, :polymorphic => true
end

class Page < ActiveRecord::Base
  has_one :menu, :as => :menuable
end

class Links < ActiveRecord::Base
  has_one :menu, :as => :menuable
end

I want to link to a polymorphic class in the Menu view using link_to, eg我想使用link_to链接到菜单视图中的多态类,例如

<%= link_to menu.name, menu.menuable %>

This works, but this retrieves the menuable object from the database, when all I wanted is to generate a link.这是有效的,但是当我想要的只是生成一个链接时,它会从数据库中检索可菜单对象。 You can imagine if my menu is large, this will really bog down my application.你可以想象,如果我的菜单很大,这真的会让我的应用程序陷入困境。

When I decared the menuable field as polymorphic, Rails created menuable_type and menuable_id.当我将 menuable 字段定义为多态时,Rails 创建了 menuable_type 和 menuable_id。 What can I use to generate a link to the polymorphic page, short of writing a helper function with a giant switch statement (eg if I have a large number of menuable 'subclasses'?)我可以用什么来生成多态页面的链接,而不是用一个巨大的 switch 语句编写一个辅助函数(例如,如果我有大量可菜单的“子类”?)

It's been long since the question was asked but I had the same problem recently and the solution was to use polymorphic_url . 提出问题已经很久了,但是最近我遇到了同样的问题,解决方案是使用polymorphic_url You need to find the name of the route you need to create a link to, for example "fast_car_path" and make it out of your *_type and *_id from polymorphic table. 您需要找到创建链接所需的路线名称,例如“ fast_car_path”,并从多态表的* _type和* _id中找出它。 For example, you have a list of comments and want to make the link to the cars that they belong to. 例如,您有一个注释列表,并想链接到它们所属的汽车。 So if *_type = FastCar we have 所以如果* _type = FastCar,我们有

@comments.each do |comment|
  link_to polymorphic_url(comment.commentable_type.tableize.singularize, :id => comment.commentable_id) 

which will generate "fast_car_path" without downloading the cars from database. 它将生成“ fast_car_path”,而无需从数据库下载汽车。

I am a noob in rails and I dont know how good that advice is, but I hope it will be helpful for somebody. 我是菜鸟,我不知道该建议有多好,但是我希望它对某人有帮助。

You could do something like this: 您可以执行以下操作:

def my_menu_url(menu)
  "/#{menu.menuable_type.tableize}/#{menu.menuable_id}"
end

if you use the rails convention for naming the controllers that correspondent to your models. 如果您使用rails约定来命名与模型相对应的控制器。

But don't do it. 但是不要这样做。 You work around the routing mechanism of rails and that's simply bad practice. 您需要解决Rails的路由机制,这简直是错误的做法。

You should use the :include option in your finders to eager load your menuables: 您应该在查找器中使用:include选项来渴望加载菜单:

Menu.all :include => :menuable

In the case this isn't enough you may use some sort of caching. 如果这还不够,您可以使用某种缓存。

Another approach could be to use url_for[menu.menuable, menu] .另一种方法是使用url_for[menu.menuable, menu] So, the link tag would look like so: <%= link_to menu.name, url_for([menu.menuable, menu]) %> .所以,链接标签看起来像这样: <%= link_to menu.name, url_for([menu.menuable, menu]) %>

you could use polymorphic routes for this你可以为此使用多态路由

https://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html https://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html

<%= link_to menu.name, polymorphic_path(menu.menuable) %>

it will generate html like this它会像这样生成 html

<a href="/menu.menuable_type/menu.menuable_id">menu.name</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM