Default segment name in rails resource routing

I want to create a route in my rails application line by line

/panda/blog
/tiger/blog
/dog/blog

where panda, tiger and dog are all constant (for the class of animals)

The usual way to do this

map.resources :animals do |animal|
 animal.resource :blog
end

will create routes along the lines

/animals/panda/blog
/animals/tiger/blog
/animals/dog/blog

But I do not want the first segment, since it will always be the same.

I know that I can do this using manual routing, but I want to know how to do this using rail resources, because I need animals and blogs.

+3
source share
2 answers

In rails 3.x, you can add path => ""to any call resourceor resourcesto remove the first segment from the generated path.

resources :animals, :path => ""

$ rake routes

    animals GET    /                   {:action=>"index", :controller=>"animals"}
            POST   /                   {:action=>"create", :controller=>"animals"}
 new_animal GET    /new(.:format)      {:action=>"new", :controller=>"animals"}
edit_animal GET    /:id/edit(.:format) {:action=>"edit", :controller=>"animals"}
     animal GET    /:id(.:format)      {:action=>"show", :controller=>"animals"}
            PUT    /:id(.:format)      {:action=>"update", :controller=>"animals"}
            DELETE /:id(.:format)      {:action=>"destroy", :controller=>"animals"}
+6
source

Source: https://habr.com/ru/post/1698028/


All Articles