37d67924a83f566ba8220348a3e9bedb

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.

# 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 !

3dcfe148e29aba731693d7b61e790243

Indrek

March 11, 2010, March 11, 2010 21:22, permalink

1 rating. Login to rate!
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) %>
D41d8cd98f00b204e9800998ecf8427e

steved

March 14, 2010, March 14, 2010 01:30, permalink

No rating. Login to rate!
# 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) %>

Your refactoring





Format Copy from initial code

or Cancel