繁体   English   中英

如何让路由助手使用现有参数?

[英]How do I have route helpers use existing params?

有时我不必为路由助手提供参数,它会自动从现有参数中提取它。 我似乎无法弄清楚如何让它始终如一地工作。

路线.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

铁路路线:

     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

这会将参数添加到我的所有路由辅助方法中,因此我不必每次都指定它。

暂无
暂无

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

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