Hello, i have table AuditLog with fields including: audited_id | audited_type That results in data like: 108 | Photo 303 | Comment What I want to do is create a link to the item, so for the example above: <a href="/photos/108">here is the photo</a> I''m trying to use a polymorphic_path but am getting an error: "undefined method `model_name'' for Fixnum:Class" When using: <%= link_to ''Here she is'', polymorphic_path([audited_id, audited_type]) %> Ideas? Thanks -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Oct-01 17:32 UTC
Re: polymorphic_path - How to Create One given a table
On Fri, Oct 1, 2010 at 11:42 AM, nobosh <bhellman1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, i have table AuditLog with fields including: audited_id | > audited_type > > That results in data like: > 108 | Photo > 303 | Comment > > What I want to do is create a link to the item, so for the example > above: > > <a href="/photos/108">here is the photo</a> > > I''m trying to use a polymorphic_path but am getting an error: > "undefined method `model_name'' for Fixnum:Class" > > When using: > > <%= link_to ''Here she is'', polymorphic_path([audited_id, > audited_type]) %> > > you have to pass 2 objects to polymorphic_path(parent_model_object,object) , audited_id is a number , that is what Fixnum:Class means, polymorphic path assembles a helper method like this, polumorphic_path(audit.auditable, audit ) =====> takes the first object passed as audit.auditable, because auditable is the alias for the parent model, so in your case is trying to get the model out of that first parameter which is a number, when it should be instance of Photo or comment, so it can create this => photo_audit_path , you see? this happens if you pass an instance of the parent model and then the instance of the polymorphic model. you are passing a number and a string, auditer_id is a number and audited_type is a string. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Interesting, so what I have available is: audited_id = 33 audited_type = Photo So using the polymorphic_path how do I take what I have from the DB AuditLog record and pass an instance of Photo? <%= link_to ''Click to See It!'', polymorphic_path([record.audited_id, record.audited_type]) %> ? Maybe there is a better way to make the link : "/photos/33" based on the data I have? Something like /<%audited_type.pluralize%>/<%audited_id%> Any suggestions? Or is polymorphic_path the right way to go? Thanks, Brett On Oct 1, 10:32 am, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Fri, Oct 1, 2010 at 11:42 AM, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hello, i have table AuditLog with fields including: audited_id | > > audited_type > > > That results in data like: > > 108 | Photo > > 303 | Comment > > > What I want to do is create a link to the item, so for the example > > above: > > > <a href="/photos/108">here is the photo</a> > > > I''m trying to use a polymorphic_path but am getting an error: > > "undefined method `model_name'' for Fixnum:Class" > > > When using: > > > <%= link_to ''Here she is'', polymorphic_path([audited_id, > > audited_type]) %> > > > you have to pass 2 objects to polymorphic_path(parent_model_object, > > object) , audited_id is a number , that is what Fixnum:Class means, > polymorphic path assembles a helper method like this, > > polumorphic_path(audit.auditable, audit ) =====> takes the first > object passed as audit.auditable, because auditable is the alias for the > parent model, so in your case is trying to get the model out of that first > parameter which is a number, when it should be instance of Photo or comment, > so it can create this => photo_audit_path , you see? this happens if you > pass an instance of the parent model and then the instance of the > polymorphic model. > > you are passing a number and a string, auditer_id is a number and > audited_type is a string.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2010-Oct-01 19:04 UTC
Re: Re: polymorphic_path - How to Create One given a table
On Fri, Oct 1, 2010 at 2:15 PM, nobosh <bhellman1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Interesting, so what I have available is: > audited_id = 33 > audited_type = Photo > > So using the polymorphic_path how do I take what I have from the DB > AuditLog record and pass an instance of Photo? >hum , i thought i was clear.... oh well polymorphic_path(audit.auditable, audit ) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ah shoot, I think the reason it wasn''t working is I had some records in the table that were empty for the polymorphic assoc... I guess it fails if it''s blank.. thank you! On Oct 1, 12:04 pm, radhames brito <rbri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Fri, Oct 1, 2010 at 2:15 PM, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Interesting, so what I have available is: > > audited_id = 33 > > audited_type = Photo > > > So using the polymorphic_path how do I take what I have from the DB > > AuditLog record and pass an instance of Photo? > > hum , i thought i was clear.... oh well > > polymorphic_path(audit.auditable, audit )-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.