2016-10-08 3 views
1

인증을 위해 Devise를 사용하고 있습니다. 사용자에게 하나의 프로필과 프로필이있는 두 개의 모델이 있습니다.devise current_user vs user_id가 params, 중첩 된 리소스로 전달되었습니다. 업데이트 동작

class User < ActiveRecord::Base 
    has_one :profile, dependent: :destroy 
end 

class Profile < ActiveRecord::Base 
    belongs_to :user 
end 

resources :users do 
    resource :profile 
end 

내가이 잘 때문에 느끼지 않는다

# e.g. users/123/profile 
current_user.profile.update(profile_params) 

를 다음을 수행 prifile#new에 루트가 등

사용자 프로필을 업데이트하려면 내가 접두사 new_user_profile_path(current_user)를 사용하여 새 사용자 프로필을 만들려면 프로필 paramsthe user_id => 123을 사용하고 있지 않습니다. 대신 user_id로 사용자 프로필을 찾아야합니까? 예 :

@profile = Profile.find_by(user_id: params[:user_id]) 
@profile.update(profile_params) 

또한 사용자는 다른 사용자의 프로필을 수정할 수 없습니다.

의견에 감사드립니다.

+0

http://weblog.jamisbuck.org/2007/2/5/nesting-resources – max

답변

1

current_user.profile.update(profile_params)은 현재 사용자의 프로필을 업데이트하는 데 사용할 수있는 방법입니다.

이렇게하면 다른 사용자가 프로필을 편집하는 것을 방지하는 데 도움이됩니다. 쿼리 문자열에서 전달 된 매개 변수에 사용자 ID를 기반으로하는 경우 보안되지 않아 로그인 한 사용자가 다른 사용자 프로필을 업데이트 할 수 있습니다.

예를 들어 평온한 경로를 사용하면 액세스 권한이있는 모든 사용자가 자신의 ID가 아니더라도 /users/profiles/:id에 게시 할 수 있습니다.

current_user은 User 모델의 인스턴스이며 이미 user_id 특성을 포함합니다.

+0

이것은 많은 의미가 있으며 실제로 허용 된 매개 변수에서 user_id를 제외했습니다. –