【问题标题】:How to remove the prefix generated in routes.rb for the params in the path如何删除在 routes.rb 中为路径中的参数生成的前缀
【发布时间】:2015-08-12 05:34:42
【问题描述】:

目前在我的路线中:

  # USER RESOURCES
  resources :users do
    resources :repositories
    patch 'change_password'
    get 'account_setting'
  end

account_setting 操作生成此路径:

user_account_setting GET    /users/:user_id/account_setting(.:format)       users#account_setting

我想要的是拥有:

user_account_setting GET    /users/:id/account_setting(.:format)       users#account_setting

两者本质上是一样的,但第一个有一个user_ 前缀,用于rails 添加的id,因为它位于用户资源块中。

旁注

我知道我可以简单地从用户资源块中删除 account_setting 操作并写入:

get 'users/:id/account_setting', to: 'users#account_setting'

但我不想。

【问题讨论】:

    标签: ruby-on-rails ruby routes nested-routes


    【解决方案1】:

    你可以这样做:

     resources :users do
          member do
            get 'account_setting'
          end
        end
    

    要添加成员路由,请将成员块添加到资源块中。

    有关文档,您可以查看http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Resources.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 2016-09-28
      • 2014-01-04
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多