[Rails] Custom view helpers

Nickolay Kolev nmkolev at uni-bonn.de
Wed Feb 1 09:32:56 GMT 2006


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


More information about the Rails mailing list