#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.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
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
Thanks Jeff !
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
&:save
and 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.
This is using symbol to proc. &:save is converted to {|obj| obj.save}. That means that any message you can send to an object is sendable. Really cool stuff! Use it in things like
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?
How would you go about adding the ID after the name if it is not unique?
This is great and works on local, but throws a 500 on production for some reason. I'm thinking it has something to do with what he mentions around ~08:09
"... In a real application you'll want to check the entire path to make sure that the parents are correct"
and then he talks about the shortcut method. Any ideas for what the non-shortcut method would be? I have a Node model I would like to be invisible as parent
Never mind. . . it was a stupid oversight; my deploy script doesn't run migrations automatically (which I gotta fix) so the fix was simply to run a manual migration on prod:
followed the tutorial, but for the life of me cannot get this to work on rails 3.2.13 and ruby 2.0.0.
basically i get the following error with the controller's 'show' action:
and from what i understand there is problem with this line in:
(gem) activerecord-3.2.13/lib/active_record/relation/finder_methods.rb
Any help appreciated. by the way, had the same error when tried to implement friendly_id gem.
Thanks Ryan
This episode is updated for Rails 5 as a blog post Model Name in URL