# Assigning a method argument to a model attribute

**URL:** <https://www.ruby-forum.com/t/assigning-a-method-argument-to-a-model-attribute/147398>\
**Category:** Rails\
**Created:** [September 1, 2008, 12:56pm UTC](https://www.ruby-forum.com/t/assigning-a-method-argument-to-a-model-attribute/147398 "2008-09-01T12:56:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tyliong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9a140/32.png) [@tyliong](https://www.ruby-forum.com/u/tyliong)\
**Post date:** [September 1, 2008, 12:56pm UTC](https://www.ruby-forum.com/t/assigning-a-method-argument-to-a-model-attribute/147398/1 "2008-09-01T12:56:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tyliong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9a140/32.png) [@tyliong](https://www.ruby-forum.com/u/tyliong)\
**Post date:** [September 1, 2008, 1:04pm UTC](https://www.ruby-forum.com/t/assigning-a-method-argument-to-a-model-attribute/147398/2 "2008-09-01T13:04:19Z")

</div>

On Mon, Sep 1, 2008 at 12:55 PM, tyliong [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

---

<div class="post-metadata">

**Author:** ![tyliong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9a140/32.png) [@tyliong](https://www.ruby-forum.com/u/tyliong)\
**Post date:** [September 1, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/assigning-a-method-argument-to-a-model-attribute/147398/3 "2008-09-01T13:09:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tyliong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e9a140/32.png) [@tyliong](https://www.ruby-forum.com/u/tyliong)\
**Post date:** [September 1, 2008, 1:11pm UTC](https://www.ruby-forum.com/t/assigning-a-method-argument-to-a-model-attribute/147398/4 "2008-09-01T13:11:59Z")

</div>

Thanks guys it works now.

On Sep 1, 7:07 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
