#63 Model Name in URL (revised)
Dec 16, 2012 | 9 minutes | Active Record, Routing
A model's ID in the URL is not very helpful to the user. Consider adding the name which can also improve SEO. Learn how to override to_param, add a slug attribute, and make a catch all route for deep nesting.



It's good to have a
constraints: { format: :html }so images and other files 404 correctly. Otherwise you're going to get a lot of 500'shmm can you elaborate Jeff?
For me this article was useful: http://robotslacker.com/2012/01/rails-3-routes-configuration-dynamic-segments-constraints-and-scope/?replytocom=123
For simple to_param overrides that append a name to the ID, I wrote a really simple gem called pretty_param. You can check out the logic https://github.com/kaiuhl/pretty-param/blob/master/lib/pretty_param.rb, but essentially it's just a better DSL for the most common to_param overrides with better filtering of special characters.
If you aren't creating too many of these routes I think the best way to do this is with static routes - e.g. get "/pricing" => "pages#pricing". It isn't strictly restful, but I think people sometimes go overboard with that. Not only is this simpler, but it also avoids the catch-all route issue mentioned as well as database performance issues brought up.
It would be neat if you showed how to make the slugs unique only to their parents and not site wide.
Try this:
Sorry, but you just blew my mind with
Page.find_each(&:save)I knew about find_each for batch loading, but what the heck is
&:saveand where else can I use it?I mean, obviously I can see what it's doing, but can I use this anywhere a block is expected? When can't you use this notation?
As long as you're calling a method directly on the object being iterated over (ie. Page#save) this will work.
I've been looking for a tutorial like this everywhere. I'm having an issue though. Whenever I go into my url and put /products/pricing (for example) it loads, but the url drops 'products' so its just /pricing. I followed the example code line by line, and then copied and pasted the code from the site into my project. Any thoughts?
Try to wrap your pricing resource in a namespace in your config/routes.rb like this
namespace :products do
... your pricing resource ..
end
here's the railsdoc
http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
Fantastic! This is so helpful, Ryan. Thanks.
If I change the name of the record from A to B, I would like something like /products/A and products/B to work.
Is there an easy way to add this history functionality using this method, similar to what's included the Friendly_ID gem?
I am building and application which uses following railscast of Ryan
1. Authentication from scratch
2. Cancan for authorization
3. Multitenancy with scope.
4. Model name in url
It was working fine till the Multitenancy with scope but after implementing model-name in url i get following error in show action.
Couldn't find Article with id=batman-robin [WHERE "articles"."tenant_id" = 1]
If I comment out "load_and_authorize_resource" from controller the show action starts working. Can someone help me out with it?
First sign in through GitHub to post a comment.