DRYing link_to with a symbol

Hi, I just put in a patch that allows you to DRY up this:

<%= link_to @company.name, @company %>

to this:

<%= link_to :name, @company %>

The symbol indicates the method to be called on the object passed in the link_to options.


Totally awesome! +1

Nice, but IMHO, this would be more useful:

class Company < AR::Base    def to_s      name    end end

<%= link_to @company %>

That's the pattern I've been using, but my link_to's look like this:

<%= link_to @company, @company %>

link_to already accepts a symbol as the first param and converts it into a string. This change would break the existing api.


I'm using the same pattern too.

Shouldn't this be encouraged? It's a way of saying: you should provide a human-readable representation of your object (yeah, usually just alias name).

I do find link_to @company.name, @company very unDRY... and even more when it gets duplicated all over the place...

Yes! Something who's been thinking alike.


I like the leveraging of the #to_s. The syntax of <%= link_to @company, @company %> isn't as DRY as it could be, so I put in a patch that allows you to do this:

<%= link_to @person %> <%= link_to [@company, @person] %>

The link text is the #to_s of the object, or if an Array is passed, the #to_s of the last object in the array.

It would be nice if ActiveRecord objects had a readable default #to_s -- even if it were something like "Person 12". Or, if the object responded to :title or :name, one of those could be used.

Forgot to post the link -- patch is here: http://dev.rubyonrails.org/ticket/8794

Which patch are you for? #8789 or #8794


I've uploaded a new patch with some minor modification to the existing patch. I gyess it makes more sense to join .to_s values of @company, @perso in case of <%= link_to [@company, @person] %>

Thanks, Pratik

Everyone jump onto #rails-contrib, ya'll seem to be online right now.

My vote is for the latter. The former breaks backwards compatibility (like Steven already noticed), and on common models there is rarely more than one attribute ready for link text output. Hacking to_s makes much more sense. (See my pastie)

I've had that in my plugin, but I like your defaults to name and title better :slight_smile:



You might consider the syntax <%= link_to &:name, @company %> instead of just a symbol, since it fits with existing idioms and wouldn't break backwards compatibility.

Thanks, Mislav!

I created a pastie of the to_s-oriented patch, so that others could test out the DRY-ed link_to syntax: Parked at Loopia

With both of these pasties applied, I've been refactoring my link_to calls -- ex: <%= link_to @post %> links to the resource, with the @post.title as the link text -- very nice.

That isn't valid Ruby syntax.


Don't you all mean

  <%= link_to h(@company.name), ... %>

Where's the escaping?!

Good catch on the html escaping.

I updated the patch so that html escaping happens automatically when the first argument passed in is not a string.

So, with <%= link_to @company %> the link text will be automatically escaped.

With the standard syntax, you'd need to explicitly escape, as before: < %= link_to h(@company.name), @company %>


... or rather, link text is escaped *only if* first argument is an ActiveRecord object, so as not to break any existing behavior.

Patch updated, and pastie updated: Parked at Loopia