在 Rails4 的 Routing 资源中更改 parent :parent_id 参数的名称
Change the name of parent :parent_id parameter in Routing resources for Rails4
我可以使用 in this way 更改路由中 :id 参数的名称,但这可以更改嵌套资源的参数,就像我有
resources :companies, param: :company_id do
resources :shares, only[:index]
end
这将生成类似
的路线
/companies/:company_company_id/shares
错了我想要这样的路线
/companies/:company_id/shares
我需要做什么?
我以前遇到过这种情况,并通过以下方法解决了这个问题……虽然它很丑陋,但我还没有找到更好的方法。
变化:
resources :companies, param: :company_id do
resources :shares, only: [:index]
end
收件人:
(注意空格only: []
)
resources :companies, param: :company_id
resources :companies, only: [], param: :id do
resources :shares, only: [:index]
end
现在当你 运行 rake routes
你会看到正确的:
/companies/:company_id/shares(.:format)
除了所有其他 companies
端点:
/companies(.:format)
/companies(.:format)
/companies/new(.:format)
/companies/:company_id/edit(.:format)
/companies/:company_id(.:format)
/companies/:company_id(.:format)
/companies/:company_id(.:format)
/companies/:company_id(.:format)
全部保持相同的 :company_id
参数名称。
更简洁的方法是使用 member
resources :companies, param: :company_id do
member do
resources :shares, only[:index]
end
end
我可以使用 in this way 更改路由中 :id 参数的名称,但这可以更改嵌套资源的参数,就像我有
resources :companies, param: :company_id do
resources :shares, only[:index]
end
这将生成类似
的路线/companies/:company_company_id/shares
错了我想要这样的路线
/companies/:company_id/shares
我需要做什么?
我以前遇到过这种情况,并通过以下方法解决了这个问题……虽然它很丑陋,但我还没有找到更好的方法。
变化:
resources :companies, param: :company_id do
resources :shares, only: [:index]
end
收件人:
(注意空格only: []
)
resources :companies, param: :company_id
resources :companies, only: [], param: :id do
resources :shares, only: [:index]
end
现在当你 运行 rake routes
你会看到正确的:
/companies/:company_id/shares(.:format)
除了所有其他 companies
端点:
/companies(.:format)
/companies(.:format)
/companies/new(.:format)
/companies/:company_id/edit(.:format)
/companies/:company_id(.:format)
/companies/:company_id(.:format)
/companies/:company_id(.:format)
/companies/:company_id(.:format)
全部保持相同的 :company_id
参数名称。
更简洁的方法是使用 member
resources :companies, param: :company_id do
member do
resources :shares, only[:index]
end
end