Assigning a method argument to a model attribute

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?

On Mon, Sep 1, 2008 at 12:55 PM, tyliong [email protected] wrote:

model.x

end

You 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?

On 1 Sep 2008, at 11:55, tyliong wrote:

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

Thanks guys it works now.

On Sep 1, 7:07 pm, Frederick C. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs