For example. There is such a route:

resources :cats 

He will give the following ways:

 /cats /cats/:id /cats/:id/edit ... 

Is it possible to redefine it so that instead of id it should receive from the database, for example, its name? That is, the paths of such routes:

 resources :cats do resources :kittens end 

Looked like this:

 /cats/:cat_name/kitten/:kitten_name /cats/:cat_name/kitten/:kitten_name/edit ... 

And another related question. Is it possible to create resources without additional words in the paths? Again, for example, if there are such routes:

 resources :cats do resources :kittens end 

So that the paths look like this:

 /:cat_name/:kitten_name /:cat_name/:kitten_name/edit ... 

Or do you have to manually make such paths with get, post, patch, and delete?

This was something that both Rails Guides and Rails Api were making. It seems that I did not find anything sensible, but it is quite likely that I missed something or did not understand it so well.

    1 answer 1

    You can, of course. You just need to know the options you need :

     resources :cats, param: 'name', path: '' 

    :path
    Allows you to change the path to the resource.

    :param does not seem to be documented, but is present in the RESOURCE_OPTIONS constant. It seems that he behaves normally only in the absence of nesting of resources and the presence of a non-empty prefix in the path ( details in the chat ).

    Therefore, such flexible routes (two /сегмента any format from the root) may have to be beaten into several parts using the :only key, carefully monitoring the results and remembering that the routes are checked in the order listed.

    • And what does the path option give? Why did you leave it empty? - smellyshovel
    • > Allows you to change the path for the resource. Is that the part before the parameter? Like / * cats * /: cat_id? - smellyshovel
    • @smellyshovel is very easy to verify. You leave only this line in routs and see the output of rake routes . - D-side
    • I know that. Just wanted to clarify everything first in words. I'll check it all out now. - smellyshovel
    • A very strange conclusion was: imgur.com/a/D4vag First, now you can’t get access to creating a new category, because this route /new is at the bottom. And secondly, it is not clear why in the category before hunt_name there is a prefix :category , and in a post it is simple :hunt_name - smellyshovel