The Rails Way: link_to Helpers

| Comments (0)
RubyOnRails_HomePage.png

I have a question regarding link_to helpers.  When do you use them and when do you not use them.

Example.
Use basic HTML:

<a href="/foobar/10">FooBar</a>                                               
or instead use a helper:

<%= link_to "FooBar", { :action => "foobar", id => 10 } %>                    
Both return the same result.  Is it strictly a preference?  I find the syntax of these helpers so forgettable and non-intuitive.  I'm not the smartest kid in the class, but every time I need to add a class name to the link so that the CSS style takes affect, I find myself googling "How do I add class names to link_to calls".  It's a pain.  It's my pain, but it's a pain.

Also, what about link_to_remote?  What if I have a loop in my view and I'm outputting 10 or 20 list items that each have the link_to_remote call?  Do I really want the JS function created 20 times?  1 for each call?  

Obviously DHH and his crew of merry men have a reason and a purpose for these helpers, otherwise they wouldn't be there, or at least they wouldn't be called helpers, they'd be called annoyers, but somewhere in our web apps we're going to have to write some HTML code.  Or is there a create_linebreak_here helper that inserts a <br /> for you?

What is the best practice when it comes to these little do-dads?

Resource Links:

Leave a comment

About this Entry

This page contains a single entry by deadprophet published on March 26, 2008 6:50 AM.

Fake Interview: Billy Mays was the previous entry in this blog.

Remembering the Conchords - A sad day for all of New Zealand is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.