# In helper
def show_row(label, attribute)
"<p><strong>#{label}:</strong> #{attribute}</p>"
end
# in controller
@user = User.first
# in view
show_row('Full Name', @user.full_name)
# Would PREFER...
show_row(@user.full_name) # => "<p><strong>Full Name:</strong>Jimbo Jones</p>"
Refactorings
No refactoring yet !
Indrek
March 11, 2010, March 11, 2010 21:22, permalink
def show_row(model, attribute)
"<p><strong>#{model.class.human_attribute_name(attribute)}:</strong> #{model.read_attribute(attribute)}</p>"
end
<%= show_row(@user, :full_name) %>
steved
March 14, 2010, March 14, 2010 01:30, permalink
# In helper
def show_row(object, method)
label = content_tag(:strong, method.to_s.titleize + ":")
value = object.send(method)
content_tag(:p, "#{label} #{value}")
end
# in controller
@user = User.first
# in view
<%= show_row(@user, :full_name) %>
Assume you have a model, like Users. An in it you have something like "full_name" as a column. I want to be able to pass to a helper method simply the variable for that attribute and display BOTH the value and the column_name. I currently have to pass a label but this seems dumb.