# Custom view helpers

**URL:** <https://www.ruby-forum.com/t/custom-view-helpers/99685>\
**Category:** Rails\
**Created:** [February 1, 2006, 10:52am UTC](https://www.ruby-forum.com/t/custom-view-helpers/99685 "2006-02-01T10:52:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nmk](https://www.ruby-forum.com/u/nmk)\
**Post date:** [February 1, 2006, 10:52am UTC](https://www.ruby-forum.com/t/custom-view-helpers/99685/1 "2006-02-01T10:52:42Z")

</div>

Hi all,

I would like to write some custom helpers like the ones available as  
form helpers text\_field, text\_area and the like. My first try was this:

custom\_helper(obj, meth)  
‘some\_string’ + obj.send(meth) + ‘some\_other\_string’  
end

Object and method are being passed as symbols like this:

custom\_helper(:person, :name)

and Ruby rightfully complains that the symbol person does not have a  
method named name. Second try was this. @person is an instance  
variable of the controller, right?

custom\_helper(obj, meth)  
‘some\_string’ + self.get\_instance\_variable(obj).send(meth) +  
‘some\_other\_string’  
end

and I get “`person’ is not allowed as an instance variable name”.

How does Rails do that? I can see the text\_field being Implemented  
like this:

InstanceTag.new(object, method, self)

but I have no idea what the InstanceTag class does or where I can  
find it.

Many thanks in advance for any pointers!

–Nicky

---

<div class="post-metadata">

**Author:** ![nmk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nmk](https://www.ruby-forum.com/u/nmk)\
**Post date:** [February 1, 2006, 10:52am UTC](https://www.ruby-forum.com/t/custom-view-helpers/99685/2 "2006-02-01T10:52:43Z")

</div>

2006/2/1, Nickolay K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi all,
> 
> I would like to write some custom helpers like the ones available as  
> form helpers text\_field, text\_area and the like. My first try was this:
> 
> custom\_helper(obj, meth)  
> ‘some\_string’ + obj.send(meth) + ‘some\_other\_string’  
> end

In your views just call custom\_helper(@person, :name)

---

<div class="post-metadata">

**Author:** ![nmk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nmk](https://www.ruby-forum.com/u/nmk)\
**Post date:** [February 1, 2006, 10:52am UTC](https://www.ruby-forum.com/t/custom-view-helpers/99685/3 "2006-02-01T10:52:45Z")

</div>

> In your views just call custom\_helper(@person, :name)

That was kind of obvious. I want to know how the ‘official’ helpers  
are implemented so that they can work with symbols as well as stings  
being passed as parameters.

Nicky

---

<div class="post-metadata">

**Author:** ![nmk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nmk](https://www.ruby-forum.com/u/nmk)\
**Post date:** [February 1, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/custom-view-helpers/99685/4 "2006-02-01T13:49:45Z")

</div>

Source code: actionpack/lib/action\_view/helpers/form\_helper.rb

2006/2/1, Nickolay K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![nmk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nmk](https://www.ruby-forum.com/u/nmk)\
**Post date:** [February 8, 2006, 7:49am UTC](https://www.ruby-forum.com/t/custom-view-helpers/99685/5 "2006-02-08T07:49:11Z")

</div>

> That was kind of obvious. I want to know how the ‘official’ helpers  
> are implemented so that they can work with symbols as well as  
> stings being passed as parameters.

I had worked around it when I needed it but stumbled across the  
solution today. This is an excerpt from code for the  
error\_messages\_for method:

instance\_variable\_get("@#{obj.to\_s}")

I had not realised the parameter to instance\_variable\_get should be a  
string or a symbol with an @-sign up front. I guess I should have  
looked it up first, it is well documented in the Pickaxe book.

Nicky
