为什么 Devise registrations edit 操作不需要参数?

Why doesn't the Devise registrations edit action need params?

我正在学习 Rails 课程,我的路线中有这个

devise_for :users,
             path: '',
             path_names: { sign_up: 'register', sign_in: 'login', edit: 'profile', sign_out: 'logout' },
             controllers: { omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations' }

这将生成类似

的路由
 edit_user_registration GET      /profile(.:format)      registrations#edit

在我看来,我只需要使用 edit_user_registration_path,而通常它应该像 user_path(:id) 这样的东西。如果我写 edit_user_registration_path(1) 它将重定向到 .../profile.1

据我所知,正常的 Rails 编辑路线应该有 params[:id]。为什么在这种情况下它不需要以及 edit_user_registration_path(1) 如何生成 .../profile.1?

路由不需要也不采用 ID 参数,因为它们作用于存储在会话中的当前用户,而不是通过参数传递。

同样,如果您想创建一个控制器来处理属于当前用户的项目,您可以这样做:

scope :user do
  resources :items, controller: :user_items
end

class UserItemsController
  before_action :authenticate_user
  # GET /user/items
  def index
    @items = current_user.items
  end
end

如果您改为构建管理界面之类的东西,您可以在其中编辑系统上的其他用户,则需要 id 参数。