Hi, when i do this: <%= method(x) %> in the view and the method: is def method(x) model.x end rails give the error of no argument. how do i assign an argument to the model? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mon, Sep 1, 2008 at 12:55 PM, tyliong <tyliong-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> when i do this: > > <%= method(x) %> > > in the view > > and the method: is > > def method(x) > > model.x > > endYou can do it like this model.send(x) Nevertheless, you normally write <%=h @model.x %> in the view, or prepare some @x in the controller that holds model.x, depending on how important is @model to the view. Do you really need that level of indirection? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-01 11:07 UTC
Re: assigning a method argument to a model attribute
On 1 Sep 2008, at 11:55, tyliong wrote:> > Hi, > > when i do this: > > <%= method(x) %> > > in the view > > and the method: is > > def method(x) > > model.x > > end >First off there''s an internal ruby method called "method" so it''s best to avoid that. Aside from that your method should be in the appropriate view helper file. Having done all that it''s still not going to work: - in side your method, model isn''t defined - assuming x contains a string and you want to call that method you can''t do it like that (what you''ve got will try and call the method called x). you need to use the send method (for example [1,2,3].send(''length'') returns 3 Fred> > rails give the error of no argument. how do i assign an argument to > the model? > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks guys it works now. On Sep 1, 7:07 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 1 Sep 2008, at 11:55, tyliong wrote: > > > > > > > Hi, > > > when i do this: > > > <%= method(x) %> > > > in the view > > > and the method: is > > > def method(x) > > > model.x > > > end > > First off there''s an internal ruby method called "method" so it''s best > to avoid that. Aside from that your method should be in the > appropriate view helper file. > > Having done all that it''s still not going to work: > - in side your method, model isn''t defined > - assuming x contains a string and you want to call that method you > can''t do it like that (what you''ve got will try and call the method > called x). you need to use the send method (for example > [1,2,3].send(''length'') returns 3 > > Fred > > > > > rails give the error of no argument. how do i assign an argument to > > the model?--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---