我有一个名为client
的模型和另一个名为client_preference
的模型。它们之间的关系是一个client
有很多client_preferences
。
现在我想创建更新和删除client_preferences
的方法。为此,我生成路线为:
map.resources :clients do |client|
client.resources :client_preferences, only: [:edit, :update, :destroy]
end
我得到以下命名路线:
edit_client_client_preference GET /clients/:client_id/client_preferences/:id/edit(.:format) {:controller=>"client_preferences", :action=>"edit"}
client_client_preference PUT /clients/:client_id/client_preferences/:id(.:format) {:controller=>"client_preferences", :action=>"update"}
DELETE /clients/:client_id/client_preferences/:id(.:format) {:controller=>"client_preferences", :action=>"destroy"}
现在,我想自定义从client_client_preference
到client_preference
的路由名称,以及生成的其他路由,以便客户端在生成的路径名中不重复两次。有没有办法做到这一点,还是我需要手动定义路线?
任何帮助或指示都将非常有用。
答案 0 :(得分:2)
使用:as
选项应该允许创建更清洁的命名助手:
map.resources :clients do |client|
client.resources :client_preferences, only: [:edit, :update, :destroy], as: 'preference'
end
现在应该映射到client_preference
。
希望它有所帮助!
答案 1 :(得分:0)
有一个关键字:as
可以帮助您在Rails中创建自定义路径名。
此问题现有答案:Rails 3 nested resources short name?