简体   繁体   English

嵌套模型的Rails路线

[英]Rails route for nested model

I have a rails model that is nested: 我有一个嵌套的Rails模型:

# 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. 它尝试转到“ / frontend / items”路径。

Is there a way to make it go to '/items' instead (without the inherited '/frontend') 有没有一种方法可以使它转到“ / items” (没有继承的“ / frontend”)

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

rake routes

There is a very nice example of precisely what you want in the rails guides: 在rails指南中有一个非常好的示例,它正是您想要的:

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

~Charles~ 〜查尔斯〜

You've explicitly namespaced a Frontend::Item as a separate model from Item . 您已将Frontend::Item明确命名为与Item分开的模型的命名空间。 Thus, a frontend_item properly routes to /frontend/items/:id . 因此, frontend_item正确地路由到/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. 请注意,这现在将与您的Item模型的路线冲突,因此您应该删除该路线。

The solution was to create a scope section: 解决方案是创建一个作用域部分:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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