<%= 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.
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 %>
+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...
Yes! Something who's been thinking alike.
http://svn.joshpeek.com/projects/plugins/stringgable_shortcuts/
<%= 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:
It would be nice if ActiveRecord objects had a readable default #to_s
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..
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/
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:
>
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:
That isn't valid Ruby syntax.
marcel
--
Marcel Molina Jr. <mar...@vernix.org>
Don't you all mean
<%= link_to h(@company.name), ... %>
Where's the 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:
Patch updated, and pastie updated: http://pastie.caboo.se/74543
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:
>
Why not name it something else that is searchable, and use a symbol or something
link_to_method(:nice_name, @person)
Agree.