Ruby on rails 如何向路由中的资源添加额外参数

Ruby on rails 如何向路由中的资源添加额外参数,ruby-on-rails,routing,routes,Ruby On Rails,Routing,Routes,我希望通过resources生成的成员路由包含附加参数 比如: resources :users 路线如下: users/:id/:another_param users/:id/:another_param/edit 有什么想法吗?你可以做一些更明确的事情,比如 get 'my_controller/my_action/:params_01/:params_02', :controller => 'my_controller', :action => 'my_action'

我希望通过
resources
生成的成员路由包含附加参数

比如:

resources :users
路线如下:

users/:id/:another_param
users/:id/:another_param/edit

有什么想法吗?

你可以做一些更明确的事情,比如

 get 'my_controller/my_action/:params_01/:params_02', :controller => 'my_controller', :action => 'my_action'

resources
方法不允许这样做。但我们可以使用
路径
选项执行类似操作,包括额外参数:

resources :users, path: "users/:another_param" 
这将生成如下URL:

users/:another_param/:id
users/:another_param/:id/edit 
 resources :users, only: [:index, :new, :create]
 # adding extra parameter for member actions only
 resources :users, path: "users/:another_param/", only: [:show, :edit, :update, :destroy]
在这种情况下,我们需要手动将另一个参数值发送到路由帮助程序:

edit_user_path(@user, another_param: "another_value")
# => "/users/another_value/#{@user.id}/edit"
传递
:如果设置了默认值,则不需要另一个参数值:

resources :users, path: "users/:another_param", defaults: {another_param: "default_value"}

edit_user_path(@user) # => "/users/default_value/#{@user.id}/edit"
或者我们甚至可以在路径中不需要额外的参数:

resources :users, path: "users/(:another_param)"

edit_user_path(@user) # => "/users/#{@user.id}/edit"

edit_user_path(@user, another_param: "another_value")
# => "/users/another_value/#{@user.id}/edit"

# The same can be achieved by setting default value as empty string:
resources :users, path: "users/:another_param", defaults: {another_param: ""}
如果我们只需要某些操作的额外参数,可以这样做:

users/:another_param/:id
users/:another_param/:id/edit 
 resources :users, only: [:index, :new, :create]
 # adding extra parameter for member actions only
 resources :users, path: "users/:another_param/", only: [:show, :edit, :update, :destroy]

我必须使用资源方法,像您那样手动添加路线不是问题。我需要避免它。我必须只使用资源方法。那么补丁actionpack。为什么必须?“我只是好奇,你的用例是什么?你搞清楚了吗?”汤姆迈阿尔瓦雷斯对我自己的问题补充道。