开发者

Is the string version of every model in Rails a URL?

In the Rails docs, it says this:

<%= url_for(@workshop) %>
  # calls @works开发者_高级运维hop.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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜