【发布时间】:2016-10-08 23:11:53
【问题描述】:
我正在使用 Devise 进行身份验证。我有两种模型,其中用户有一个个人资料,个人资料属于用户:
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
end
class Profile < ActiveRecord::Base
belongs_to :user
end
我正在使用嵌套资源,例如
resources :users do
resource :profile
end
为了创建一个新的用户配置文件,我使用前缀 new_user_profile_path(current_user) 路由到 prifile#new 等
要更新用户个人资料,我执行以下操作
# e.g. users/123/profile
current_user.profile.update(profile_params)
这感觉不对,因为我没有在个人资料 params 中使用 the user_id => 123。
我是否应该通过 user_id 来查找用户配置文件,例如
@profile = Profile.find_by(user_id: params[:user_id])
@profile.update(profile_params)
此外,用户不能编辑其他用户的个人资料。
感谢您的反馈。
【问题讨论】:
标签: ruby-on-rails devise nested-resources