resources 方法不允许这样做。但是我们可以使用包含额外参数的path 选项来做类似的事情:
resources :users, path: "users/:another_param"
这将生成这样的网址:
users/:another_param/:id
users/:another_param/:id/edit
在这种情况下,我们需要手动将:another_param 值发送给路由助手:
edit_user_path(@user, another_param: "another_value")
# => "/users/another_value/#{@user.id}/edit"
如果设置了默认值,则不需要传递 :another_param 值:
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: ""}
如果我们只需要某些动作的额外参数,可以这样完成:
resources :users, only: [:index, :new, :create]
# adding extra parameter for member actions only
resources :users, path: "users/:another_param/", only: [:show, :edit, :update, :destroy]