Topic: helper method is ugly

  def last_number_of_months(number)
    months = []
    number.times do |month|
      months << link_to(Time.now.months_ago(month).strftime("%B %Y").to_s + "<br />", archive_path+Time.now.months_ago(month).strftime("/%Y/%m"))
    end
    return months
  end

Then I call from the view.

<%= last_number_of_months(6) %>

It works, but I really don't like passing the link_to into an array. Maybe that is ok, but it looks terrible to me. Just not sure how to clean it up. Plus I'm doing + "<br />" to get my line returns. I know I'm calling Time.now.months_ago twice in a row as well and that could probably be shortened up. Real thing I'm worried about though is just passing all that cruft into the array. Thanks!

Last edited by gregf (2007-11-21 22:34:26)

Re: helper method is ugly

def last_number_of_months(number)
  number.map do |n|
    time = n.months.ago
    n = link_to(time.strftime("%B %Y"),archive_path + time.strftime("/%B/%Y"))
  end
  number.join("<br />")
end

not really shorter in lines, but more readable i think.

Last edited by Duplex (2007-11-22 08:07:47)