Link_to & others really necessary?

One of the areas where I think Rails goes too far is in many of the
helpers that (AFAICT) do nothing more than change the syntax used to
write simple HTML tags. link_to and many of the form tags come to mind.

For me, these helpers aren’t that helpful. I find the HTML much
clearer to write and read, I don’t see any added value in the
overhead costs of using code to generate a simple HTML tag. I have
written several code generators for complicated stuff like producing
sortable, paginated lists, so I’m not against generating HTML, just
not generating individual tags.

So, before I dismiss them entirely, what’s the argument for using them?

– gw

Greg W. wrote:

So, before I dismiss them entirely, what’s the argument for using them?

– gw

I guess it’s easier to read for one (IMHO) but doesn’t link_to also
allow you to create a link that’s a little more complex without thinking
about it, e.g. considering the controller and the action and the popups
and the confirms (OK, I’m just reading this off the API) and the method
(POST, delete, etc.) Also, I can’t remember if it takes care of routes
also…

Either way, I guess you can ignore (dismiss) it till it you need it :slight_smile:

Cheers,
Mohit.
11/6/2007 | 2:00 AM.

So, before I dismiss them entirely, what’s the argument for using them?

Not having to remember as much syntax for one, and knowing that if
prototype changes it’s API, I (most likely) won’t have to change my view
code…

This:

<% form_remote_tag :url => {:controller => ‘home’,
:action => ‘get_email’},
:update => ‘notice’,
:loading => “Element.show(‘indicator’);”,
:complete => “Element.hide(‘indicator’);” do %>

vs this:

Also link to will parse the arguments and build the appropriate route
which your handcoded link won’t.

Most of the form tags will automatically set/check/etc the value if
appropriate.

The asset tag helpers get you the file modification builtin and let you
easily use asset hosts.

But you’re certainly not required to use them :slight_smile:

-philip

The link_to and friends really are one of the best things about the
application.

link_to “Home”, :action=>“public”, :controller=>“index”

That’s nice, because it will build /public/index/ for me, no matter what
level of my app I’m in. I don’t have to worry about parent paths, nested
structures, etc.

But it gets better. If I define a named route in routes.rb

 map.home '', :controller=>"public", :action=>"index"

Now, without changing links, the link_to example I used above will
render a
link to /

And now that I have a named route, I can shorten that code to

link_to “Home”, home_url

That’s even better, because now someone might come along and decide that
the
homepage needs to be moved into a different controller, like “main”
because
they don’t like “public”

So I just change the route

map.home ‘’, :controller=>“main”, :action=>“index”

And all my links change.

How’s that?