DRYing link_to with a symbol

7 views
Skip to first unread message

Geoff B

unread,
Jun 28, 2007, 12:50:54 AM6/28/07
to Ruby on Rails: Core
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.

http://dev.rubyonrails.org/ticket/8789

Mislav Marohnić

unread,
Jun 28, 2007, 5:39:41 AM6/28/07
to rubyonra...@googlegroups.com
On 6/28/07, Geoff B <gbue...@gmail.com> wrote:

<%= link_to :name, @company %>

Totally awesome!
+1

Brandon Keepers

unread,
Jun 28, 2007, 7:16:01 AM6/28/07
to rubyonra...@googlegroups.com

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 %>

Steven A Bristol

unread,
Jun 28, 2007, 9:23:04 AM6/28/07
to rubyonra...@googlegroups.com
link_to already accepts a symbol as the first param and converts it into a string. This change would break the existing api.

Damian Janowski

unread,
Jun 28, 2007, 9:50:02 AM6/28/07
to rubyonra...@googlegroups.com
On 6/28/07, Brandon Keepers <bkee...@gmail.com> wrote:
> Nice, but IMHO, this would be more useful:
>
> class Company < AR::Base
> def to_s
> name
> end
> end

+1!

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...

Josh Peek

unread,
Jun 28, 2007, 11:01:06 AM6/28/07
to Ruby on Rails: Core

Yes! Something who's been thinking alike.

http://svn.joshpeek.com/projects/plugins/stringgable_shortcuts/

Geoff B

unread,
Jun 28, 2007, 11:06:07 AM6/28/07
to Ruby on Rails: Core
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.


On Jun 28, 6:16 am, Brandon Keepers <bkeep...@gmail.com> wrote:

Geoff B

unread,
Jun 28, 2007, 11:11:51 AM6/28/07
to Ruby on Rails: Core

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

Josh Peek

unread,
Jun 28, 2007, 11:19:56 AM6/28/07
to Ruby on Rails: Core
Which patch are you for? #8789 or #8794

Mislav Marohnić

unread,
Jun 28, 2007, 11:26:10 AM6/28/07
to rubyonra...@googlegroups.com
On 6/28/07, Geoff B <gbue...@gmail.com> wrote:

It would be nice if ActiveRecord objects had a readable default #to_s

http://pastie.caboo.se/74412

Pratik

unread,
Jun 28, 2007, 11:30:59 AM6/28/07
to rubyonra...@googlegroups.com
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

On 6/28/07, Geoff B <gbue...@gmail.com> wrote:
>
>


--
rm -rf / 2>/dev/null - http://null.in

Dont judge those who try and fail, judge those who fail to try..

Josh Peek

unread,
Jun 28, 2007, 11:32:32 AM6/28/07
to Ruby on Rails: Core
Everyone jump onto #rails-contrib, ya'll seem to be online right now.

Mislav Marohnić

unread,
Jun 28, 2007, 11:32:47 AM6/28/07
to rubyonra...@googlegroups.com
On 6/28/07, Josh Peek <josh...@gmail.com> wrote:

Which patch are you for? #8789 or #8794

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)

Josh Peek

unread,
Jun 28, 2007, 11:34:31 AM6/28/07
to Ruby on Rails: Core
On Jun 28, 10:26 am, "Mislav Marohnić" <mislav.maroh...@gmail.com>
wrote:

> On 6/28/07, Geoff B <gbues...@gmail.com> wrote:
>
>
>
> > It would be nice if ActiveRecord objects had a readable default #to_s
>
> http://pastie.caboo.se/74412

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

http://svn.joshpeek.com/projects/plugins/stringgable_shortcuts/

Obie Fernandez

unread,
Jun 28, 2007, 11:37:58 AM6/28/07
to rubyonra...@googlegroups.com
My2c...

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.

On 6/28/07, Josh Peek <josh...@gmail.com> wrote:
>

Geoff B

unread,
Jun 28, 2007, 1:16:10 PM6/28/07
to Ruby on Rails: Core
Thanks, Mislav!

I created a pastie of the to_s-oriented patch, so that others could
test out the DRY-ed link_to syntax: http://pastie.caboo.se/74452

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.


On Jun 28, 10:26 am, "Mislav Marohnić" <mislav.maroh...@gmail.com>
wrote:

Marcel Molina Jr.

unread,
Jun 28, 2007, 2:18:35 PM6/28/07
to rubyonra...@googlegroups.com
On Thu, Jun 28, 2007 at 11:37:58AM -0400, Obie Fernandez wrote:
> 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.

That isn't valid Ruby syntax.

marcel
--
Marcel Molina Jr. <mar...@vernix.org>

Courtenay

unread,
Jun 28, 2007, 3:48:22 PM6/28/07
to rubyonra...@googlegroups.com
On 6/28/07, Geoff B <gbue...@gmail.com> wrote:
>
> Thanks, Mislav!
>
> I created a pastie of the to_s-oriented patch, so that others could
> test out the DRY-ed link_to syntax: http://pastie.caboo.se/74452
>
> 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.
>

Don't you all mean

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

Where's the escaping?!

Geoff B

unread,
Jun 28, 2007, 4:29:17 PM6/28/07
to Ruby on Rails: Core
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 %>

http://dev.rubyonrails.org/ticket/8794

On Jun 28, 2:48 pm, Courtenay <court3...@gmail.com> wrote:

Geoff B

unread,
Jun 28, 2007, 5:13:28 PM6/28/07
to Ruby on Rails: Core
... 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: http://pastie.caboo.se/74543

Pratik

unread,
Jul 1, 2007, 8:10:20 PM7/1/07
to rubyonra...@googlegroups.com
Hi,

I've converted this functionality into a quick plugin and also added
some method_missing magic to enable form like
link_to_nice_name(@person) ( which uses @person.nice_name ).

Grab it from http://linktomagic.googlecode.com/svn/link_to_magic/

Thanks,
Pratik

On 6/28/07, Geoff B <gbue...@gmail.com> wrote:
>


--
http://m.onkey.org

Courtenay

unread,
Jul 2, 2007, 12:51:37 AM7/2/07
to rubyonra...@googlegroups.com
ack! more method missing! this means more functions people can't look
up in the API.

Why not name it something else that is searchable, and use a symbol or something

link_to_method(:nice_name, @person)

Pratik

unread,
Jul 2, 2007, 11:10:40 AM7/2/07
to rubyonra...@googlegroups.com
$ find . | grep \.rb$ | xargs grep -s 'method_missing' | wc -l
59

:-P


--
http://m.onkey.org

Evan

unread,
Jul 2, 2007, 1:16:35 PM7/2/07
to Ruby on Rails: Core

> ack! more method missing! this means more functions people can't look
> up in the API.

Agree.


Reply all
Reply to author
Forward
0 new messages