rails/rails — pull request #3256 by apocalyptiq (2011-10-08 11:42:15+00:00)
Title: Make use of #to_s: link_to(model)

We often use link_to to point to some record, for example:

link_to product.name, product

I think that it is a good approach to make use of #to_s method in models to point, how that record is represented by string by default, so for example:

class Product << ActiveRecord::Base
def to_s
name
end
end

That way, we can use that method in all links to that record:

link_to product.to_s, product

so when we will have to change that default string representation of model (for example in User model, "#{first_name} #{last_name} instead of username), we will have to change it only in #to_s of that model, so for example.

Thanks for features of #to_s, we can shorten it to:

link_to product, product

So it would be nice, if #link_to would be able to get url from body of link, if no url is given, so we would be able to do just simply:

link_to product

I thought that maybe #link_to have already such as feature, but for now it produces link to the current page with product name.

I wanted to try add such as feature, but I have problems with running Rails from Github, because of this bug: https://github.com/carlhuda/bundler/issues/1470

So I've just edited files of action_view of Rails 3.1.1.rc1 from my installed gems, and this feature works on application which is using that gem, so I think that it will work with Rails from Github.

When that bug will be fixed, I can add tests for it, but if somebody want, go ahead and check if it is working with Rails from Github and write tests for it.