link_to as a block helper
Tuesday, June 19th, 2007When it comes to creating helpers for the view portion of satisfaction, I’ve started taking the approach that once I see something “ugly” twice in the rhtml that it should be extracted out into a helper. In many cases, before that.
Today’s wart? link_to calls that have html for the text content. take for example:
<%= link_to "<strong>#{product.name}</strong><span>#{pluralize(product.topic_count(company), 'topic')}</span>", href, :class => "product_label" %>
ewww… and that doesn’t even get into having href defined above in a <% %> block. So, I extended link_to with the help of alias_method_chain such that it will take a block argument instead of the its normal first parameter.
<% link_to browse_url(product), :class => "product_label" do %> <strong><%= product.name %></strong> <span>(<%= pluralize(product.topic_count(company), 'topic') %>)</span> <% end %>
Much more readable. See the code block_link_to.rb