Rails 多态关系和 link_to
这是我的架构,
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
我想使用 link_to 链接到菜单视图中的多态类,例如,
<%= link_to menu.name, menu.menuable %>
这可以工作,但是这会从数据库中检索可菜单对象,而我想要的只是生成链接。您可以想象,如果我的菜单很大,这真的会让我的应用程序陷入困境。
当我将 menuable 字段声明为多态时,Rails 创建了 menuable_type 和 menuable_id。我可以使用什么来生成到多态页面的链接,而不用编写带有巨大 switch 语句的辅助函数(例如,如果我有大量可菜单“子类”?)
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, e.g.
<%= 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. What can I use to generate a link to the polymorphic page, short of writing a helper function with a giant switch statement (e.g. if I have a large number of menuable 'subclasses'?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个问题已经很久没有被问到了,但我最近也遇到了同样的问题,解决方案是使用
polymorphic_url
。您需要找到需要创建链接的路线名称,例如“fast_car_path”,并从多态表中的 *_type 和 *_id 中找到它。例如,您有一个评论列表,并希望链接到它们所属的汽车。因此,如果 *_type = FastCar,我们将生成“fast_car_path”,而无需从数据库下载汽车。
我是 Rails 的菜鸟,我不知道这个建议有多好,但我希望它对某人有帮助。
It's been long since the question was asked but I had the same problem recently and the solution was to use
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. 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 havewhich will generate "fast_car_path" without downloading the cars from database.
I am a noob in rails and I dont know how good that advice is, but I hope it will be helpful for somebody.
您可以这样做:
如果您使用 Rails 约定来命名与您的模型相对应的控制器。
但不要这样做。你绕过了 Rails 的路由机制,这根本就是不好的做法。
您应该在查找器中使用 :include 选项来预先加载您的菜单:
如果这还不够,您可以使用某种缓存。
You could do something like this:
if you use the rails convention for naming the controllers that correspondent to your models.
But don't do it. You work around the routing mechanism of rails and that's simply bad practice.
You should use the :include option in your finders to eager load your menuables:
In the case this isn't enough you may use some sort of caching.
另一种方法是使用 url_for[menu.menuable, menu]。因此,链接标记如下所示:
<%= link_to menu.name, url_for([menu.menuable, menu]) %>
。Another approach could be to use
url_for[menu.menuable, menu]
. So, the link tag would look like so:<%= link_to menu.name, url_for([menu.menuable, menu]) %>
.您可以为此使用多态路由
https://api.rubyonrails.org/classes /ActionDispatch/Routing/PolymorphicRoutes.html
它将生成这样的 html
you could use polymorphic routes for this
https://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html
it will generate html like this