Ruby on Rails / Ugly helper function example

From WhyNotWiki

Jump to: navigation, search

[edit] Original version (in .rhtml template)

<div>
  <% for label, date in [
    ["May 2006", "2006-05-01"],
    ["June 2006", "2006-06-01"],
    ["July 2006", "2006-07-01"]
  ] %>
  <%= link_to_unless @start_date.iso8601date == date,
        label, :month => date %> |
  <% end %>
</div>

[edit] Helper version (so it could be reused in multiple templates)

  def month_links
    html = "<div>"
    for label, date in [
      ["May 2006", "2006-05-01"],
      ["June 2006", "2006-06-01"],
      ["July 2006", "2006-07-01"]
    ] do
      html += link_to_unless(@start_date.iso8601date == date,
            label, :month => date) + " | "
    end

    html += "</div>"
  end
Personal tools