4 ms·
The routing system and associated view helpers can really get confusing. For example: link_to @story.title, @story You have to know that rails has some a
by epochwolf 12y ago
The routing system and associated view helpers can really get confusing.
For example:
link_to @story.title, @story
You have to know that rails has some automatic routing based on the class of an object. If @story is a Story class, rails basically does this underneath:
link_to @story.title, send("#{@story.class.name.downcase}_path".to_sym, @story.to_param)
There's implicit conversion of class names going on under the hood in a few places. It's all documented but it's not easy to find the documentation when you don't know what you are looking for.
The thing that really screws up people starting with rails is not understanding the various layers (html, views, controllers, models, http, etc.) and how rails puts those together. If you don't know how to do web programming with basic html and php, rails will eat you alive with it's seemingly magical behaviors.
- troels 12y agoI have to agree - The path helpers are very opaque. It would probably do Rails well to generate a app/helpers/path_helper.rb file with the actual implementations in them.
- hwhelchel 12y ago`rake routes` will output the routes and paths. Appending _path or _url to the path will generate appropriate methods.
- troels 12y agoSure. But that still doesn't tell me exactly which arguments they take. Or give me an opportunity to debug the code when it doesn't do as I expect. I realise that it just-works (tm). It's when it doesn't, it gets problematic.
- epochwolf 12y agoIt's actually pretty easy to tell the parameters they take if you look at the url for the route. For: story GET /story/:id(.:format) story#show You get story_path(id) story_path(id, format) story_url(id) story_url(id, format) In practice it doesn't cause as many problems as you think, even in large applications.