我如何让路由助手使用现有参数?
How do I have route helpers use existing params?
有时我不必向路由助手提供参数,它会自动从现有参数中提取它。我似乎无法弄清楚如何让它始终如一地工作。
routes.rb:
scope ':admin_id', module: :admin do
resources :roles
end
呈现 :admin_id
设置为 10
的页面时:
<%= roles_path %> # /10/roles
<%= edit_role_path(my_role, admin_id: 10) %> # /10/roles/15/edit
<%= edit_role_path(my_role) %> # sometimes works
rails
路线:
roles GET /:admin_id/roles(.:format) roles#index
POST /:admin_id/roles(.:format) roles#create
new_role GET /:admin_id/roles/new(.:format) roles#new
edit_role GET /:admin_id/roles/:id/edit(.:format) roles#edit
role GET /:admin_id/roles/:id(.:format) roles#show
PATCH /:admin_id/roles/:id(.:format) roles#update
PUT /:admin_id/roles/:id(.:format) roles#update
DELETE /:admin_id/roles/:id(.:format) roles#destroy
想通了!
def default_url_options(options={})
{ admin_id: params[:admin_id] }
end
这会将参数添加到我的所有路由助手方法中,因此我不必每次都指定它。
有时我不必向路由助手提供参数,它会自动从现有参数中提取它。我似乎无法弄清楚如何让它始终如一地工作。
routes.rb:
scope ':admin_id', module: :admin do
resources :roles
end
呈现 :admin_id
设置为 10
的页面时:
<%= roles_path %> # /10/roles
<%= edit_role_path(my_role, admin_id: 10) %> # /10/roles/15/edit
<%= edit_role_path(my_role) %> # sometimes works
rails 路线:
roles GET /:admin_id/roles(.:format) roles#index
POST /:admin_id/roles(.:format) roles#create
new_role GET /:admin_id/roles/new(.:format) roles#new
edit_role GET /:admin_id/roles/:id/edit(.:format) roles#edit
role GET /:admin_id/roles/:id(.:format) roles#show
PATCH /:admin_id/roles/:id(.:format) roles#update
PUT /:admin_id/roles/:id(.:format) roles#update
DELETE /:admin_id/roles/:id(.:format) roles#destroy
想通了!
def default_url_options(options={})
{ admin_id: params[:admin_id] }
end
这会将参数添加到我的所有路由助手方法中,因此我不必每次都指定它。