Rails Helpers and Instance Variables
November 10th, 2005
I was reading the code for the FileColumn, helper and I had a thought. What if we could obviate the need for requiring instance variables for helpers ?
A recent example on the mailing list ? render partial and passing in the object, by Mark Van Holstyn. In the thread, he asks why he can’t do:
1 2 3 4 |
def edit @user = User.find_by_id(1) render :partial => 'address', :object => @user.address} end |
<%= text_area('address', 'street') %> |
1 2 3 4 |
def url_for_file_column(object_name, method, suffix=nil) object = instance_variable_get("@#{object_name.to_s}") ... end |
What if we did it this way instead ?
1 2 3 4 5 6 7 8 9 |
def url_for_file_column(object_name, method, suffix=nil) object = case object_name when String, Symbol instance_variable_get("@#{object_name.to_s}") else object_name end ... end |
Actually, this should all be abstracted away in some kind of helper for helpers. Let me take a moment to prepare a patch. I think this would make things quite a bit easier for partial users.