In the Rails docs, it says this:
<%= url_for(@workshop) %>
# calls @workshop.to_s
# => /workshops/5
Is this because the string version of every model in Rails corresponds to a URL for a particular instance of that model?
In the Rails docs, it says this:
<%= url_for(@workshop) %>
# calls @workshop.to_s
# => /workshops/5
Is this because the string version of every model in Rails corresponds to a URL for a particular instance of that model?
Well, the to_s
method of Object
prints out the class name. A Rails "model" is usually something that extends ActiveRecord::Base
, and looking at the source code for that class shows it does not override to_s
. My brain is too small to figure out what the source code for url_for
is doing, but I suspect it is not just calling to_s
on a model but rather doing more work to generate that URL. In Rails, URLs come from config/routes.rb
so url_for
must be using that in some way...