2014-01-29 2 views
0

프로필이 has_one 인 사용자 모델이 있습니다.Devise - 가입시 HAS_ONE 관련 모델에 데이터 삽입

프로필은 모든 사용자 물건 (이름, 전화 번호, 주소, 주 등)이 저장되는 장소입니다.

가입하는 동안 사용자가 해당 입력란을 채우도록해야합니다.

중첩 된 필드를 시도했지만 실제로 작동하지 않으며 그 이유를 실제로 이해하지 못합니다.

비슷한 코드 예제가있는 사람이 있습니까? 인터넷에서 아무것도 찾을 수 없습니다.

Candidate has_one :profile 
Profile belongs_to :user 

등록 양식 :

= simple_form_for(:candidate, 
          as: Candidate, 
          url: candidate_registration_path) do |f| 
      = f.simple_fields_for :profile do |profile| 
      = profile.input :first_name 
      = profile.input :last_name 
      = f.input :email 
      = f.input :password 
      = f.input :password_confirmation 
      = f.submit 'Start Building', class: 'btn btn-primary' 

이 제외 컨트롤러 아무 짓도 안 했어요 :

def configure_devise_params 
    devise_parameter_sanitizer.for(:sign_up) do |u| 
     u.permit(:email, :password, :password_confirmation, 
       profile_attributes: [:first_name, :last_name]) 
    end 
    end 
+0

코드 샘플이 유용 할 것입니다. – Nikola

답변

0

당신이 말할 때 당신이 그것을하지 않는 것을 의미 할 작동하지 않습니다 구하다? 또는 필드가 표시되지 않습니까?

후자의 경우, 작업이 실행되기 전에 등록 컨트롤러에 빈 프로필을 작성해야합니다. 그래서 기본적으로 유증 컨트롤러를 무시하고 이런 일을 수행

class RegistrationsController < Devise::RegistrationsController 

    def new 
    user = build_resource({}) 
    user.build_profile if user.profile.blank? 
    respond_with self.resource 
    end 

end 

routes.rb를

devise_for :candidates, :controllers => {:registrations => "registrations"} 

Candidate.rb

has_one :profile 
accepts_nested_attributes_for :profile 

을하고 강력한을 위해 위에 쓴 코드를 확인하십시오 매개 변수가 application_controller에 있습니다.

또한 유증 모델 "후보"

+0

유일한 문제는 Devise의 기본 new_session_path를 사용하지 않는다는 것입니다. POST 요청을 통해 sessions_path에 제출하는 양식이 있습니다. – user3007832

+0

당신은 무엇을 알고 있습니다 - 나는 그것이 효과가 있다고 생각합니다. 감사 – user3007832

0

확인 로그에서 PARAMS라고 가정한다. 해당 매개 변수를 사용하여 콘솔에서 사용자를 생성 해보십시오. 사용자 모델에 accepts_nested_attributes_for :profile이 있습니까?

관련 문제