簡體   English   中英

嵌套模型的Rails路線

[英]Rails route for nested model

我有一個嵌套的Rails模型:

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

當我打電話

form_for(@frontend_item)

它嘗試轉到“ / frontend / items”路徑。

有沒有一種方法可以使它轉到“ / items” (沒有繼承的“ / frontend”)

您可能已經做到了,但是您嘗試了

rake routes

在rails指南中有一個非常好的示例,它正是您想要的:

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

〜查爾斯〜

您已將Frontend::Item明確命名為與Item分開的模型的命名空間。 因此, frontend_item正確地路由到/frontend/items/:id

要覆蓋它,請將以下行添加到您的路由文件中:

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

請注意,這現在將與您的Item模型的路線沖突,因此您應該刪除該路線。

解決方案是創建一個作用域部分:

# 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