简体   繁体   English

Rails 3-polymorphic_path-如何在给定表的情况下创建一个

[英]Rails 3 - polymorphic_path - How to Create One given a table

i have table AuditLog with fields including: audited_id | 我的表AuditLog的字段包括:audited_id | audited_type audited_type

That results in data like: 108 | 结果为:108 | Photo 303 | 照片303 | Comment 评论

What I want to do is create a link to the item, so for the example above: 我想做的是创建指向该项目的链接,因此对于上面的示例:

here is the photo 这是照片

I'm trying to use a polymorphic_path but am getting an error: "undefined method `model_name' for Fixnum:Class" 我正在尝试使用polymorphic_path,但遇到错误:“ Fixnum:Class的未定义方法'model_name'”

When using: 使用时:

<%= link_to 'Here she is', polymorphic_path([audited_id, audited_type]) %>

Ideas? 有想法吗? Thanks 谢谢

Updating with code based on the answer by Luke below: 根据以下Luke的答案更新代码:

class NewsFeed < ActiveRecord::Base
    default_scope :order => 'news_feeds.created_at DESC'    
    belongs_to :feeded, :polymorphic => true
end

class Note < ActiveRecord::Base
  has_many :newsfeed, :as => :feeded
end

In the partial which is being passed the local storyitem: 在正在传递的局部故事中,有:

<%= link_to 'Here she is', polymorphic_path(storyitem.feeded) %>

The DB migration file, contains the following line for CreateNewsFeeds 数据库迁移文件,包含CreateNewsFeeds的以下行

  t.references :feeded, :polymorphic => true

You should have a method #auditable (or whatever your polymorphic association is called) on AuditLog objects. 您应该在AuditLog对象上使用#auditable方法(或称为多态关联的任何方法)。 If you pass the result of that method to polymorphic_path it will return the correct path for you. 如果您将该方法的结果传递给polymorphic_path,它将为您返回正确的路径。

Update: 更新:

Assuming you have the following associations (or are using acts_as_auditable or something that sets up the relationships for you): 假设您具有以下关联(或正在使用acts_as_auditable或为您设置关系的某种关联):

class AuditLog
  belongs_to :auditable, :polymorphic => true
end

class AuditedObject
  has_many :audits, :as => :auditable
end

You'll be able to call auditable on any instance of AuditLog, and it will return the associated audited object. 您将能够在任何AuditLog实例上调用auditable,它将返回关联的已审核对象。 So you can call 所以你可以打电话

<%= link_to 'Here she is', polymorphic_path(audit_log.auditable) %>

to get a link to the audited object. 获取指向审核对象的链接。

So, anywhere that you have a polymorphic association in a class, there is an instance method setup with the name of that association that will return the associated object. 因此,在类中具有多态关联的任何地方,都有一个实例方法设置,带有该关联的名称,该实例方法将返回关联的对象。

Gosh, I'm hoping that makes sense. 天哪,我希望那是有道理的。 Let me know if you need me to clarify it further. 让我知道是否需要我进一步说明。

The problem with polymorphic_path it needs an object, so you first need to fetch the object from the database. polymorphic_path的问题是它需要一个对象,因此您首先需要从数据库中获取该对象。 Depending on your use case this can be a big performance problem. 根据您的用例,这可能是一个很大的性能问题。 In case of a log viewer, where you have a list of for example 100 entries, and just want to show links to the entires, you will fetch 100 objects, just to get their path. 对于日志查看器,您有一个列表,例如100个条目,并且只想显示整个链接,则将获取100个对象,只是要获取它们的路径。

I had a similar problem, my solution was to hack a method to construct the path from the class name and id: 我有一个类似的问题,我的解决方案是破解一种从类名和ID构造路径的方法:

class AuditLog
...
def audited_path
    "/#{self.audited_type.tableize}/#{self.audited_id}"
end

The method will return for example "/photos/302". 该方法将返回例如“ / photos / 302”。 But of course it will work only for quite simple routings. 但是当然,它仅适用于非常简单的路由。

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

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