简体   繁体   中英

Rails route for nested model

I have a rails model that is nested:

# app/models/frontend/item.rb
class Frontend::Item < Item
end

When I call

form_for(@frontend_item)

It tries to go to the '/frontend/items' path.

Is there a way to make it go to '/items' instead (without the inherited '/frontend')

您可能已经做到了,但是您尝试了

rake routes

There is a very nice example of precisely what you want in the rails guides:

http://guides.rubyonrails.org/routing.html#limits-to-nesting

~Charles~

You've explicitly namespaced a Frontend::Item as a separate model from Item . Thus, a frontend_item properly routes to /frontend/items/:id .

To override that, add the following line to your routes file:

# routes.rb
match 'item/:id' => 'Frontend::Item#show'

Note that this will now conflict with the route for your Item model so you should remove that route.

The solution was to create a scope section:

# config/routes.rb
scope :module => "frontend", :as => 'frontend' do
  resources :items
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM