2016-09-08 3 views
0

사용자가 회사에 속하고 회사에 많은 사용자가있을 수 있음을 구조화하고 싶습니다. 회사 이름은 내 Devise 가입 양식에 포함되어야합니다. 여기 Rails를 사용하여 학부모 협의회 등록 양식

내 코드입니다 :

company.rb

class Company < ApplicationRecord 
    has_many :users 
end 

user.rb

class User < ApplicationRecord 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 
    belongs_to :company 
    accepts_nested_attributes_for :company 
end 

registrations_controller.rb

위의 코드를 사용하여
class RegistrationsController < Devise::RegistrationsController 

    before_action :configure_sign_up_params, only: [:create] 

    # GET /resource/sign_up 
    def new 
    build_resource({}) 
    set_minimum_password_length 
    resource.build_company 
    yield resource if block_given? 
    respond_with self.resource 
    end 

    # POST /resource 
    def create 
    build_resource(sign_up_params) 
    resource.save 
    if resource.persisted? 
     if resource.active_for_authentication? 
     set_flash_message :notice, :signed_up if is_flashing_format? 
     sign_up(resource_name, resource) 
     respond_with resource, location: after_sign_up_path_for(resource) 
     else 
     set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format? 
     expire_data_after_sign_in! 
     respond_with resource, location: after_inactive_sign_up_path_for(resource) 
     end 
    else 
     clean_up_passwords resource 
     set_minimum_password_length 
     respond_with resource, location: new_api_v1_public_members_user_registration_path(beta_token: params[:beta_token]) 
    end 
    end 

    def configure_sign_up_params 
    devise_parameter_sanitizer.permit(:sign_up) do |user_params| 
     user_params.permit(:email, :password, :password_confirmation, company: [ :name]) 
    end 
    end 
end 

는 _form.haml

... 
= f.text_field :email 
= f.fields_for :company do |c| 
    = c.text_field :name 
= f.password_field :password, autocomplete: "off", autofocus: true 
= f.password_field :password_confirmation, autocomplete: "off" 
... 

, (내 로그 파일에 따라) 내 생성 경로에있는 모든 값을 제출합니다. 그러나 회사의 부모 가치는 유지되지 않습니다. 이로 인해 Company.name 값을 비워 둘 수 없으므로 양식 유효성 검사가 실패합니다.

어떻게이 코드를 변경합니까 : 아동 (사용자) 형태

  • 에 중첩되어

    1. 저장 부모 (회사)가 저장 될 때 사용자에게 Company.id 값을 할당 사용자 모델에서 'company_id'속성을 채우는 방법으로

    도움을 주시면 감사하겠습니다.

  • +0

    중첩 된 양식을 사용하려고 했습니까? https://github.com/ryanb/nested_form? 감사합니다. @luissimo. –

    답변

    1

    한번에 변경

    def configure_sign_up_params 
        devise_parameter_sanitizer.permit(:sign_up) do |user_params| 
         user_params.permit(:email, :password, :password_confirmation, company: [ :name]) 
        end 
        end 
    

    def configure_sign_up_params 
        devise_parameter_sanitizer.permit(:sign_up) do |user_params| 
         user_params.permit(:email, :password, :password_confirmation, company_attributes: [:id, :name, :_destroy]) 
        end 
        end 
    

    그리고 당신의 user.rb 모델 변화

    으로; accepts_nested_attributes_for :company
    accepts_nested_attributes_for :company, reject_if: :all_blank, allow_destroy: true 
    

    그리고 내가 HAML가 작동하지만 내가 f.fields_for 블록의 끝을 볼 방법을 모르는

    합니다.

    = f.fields_for :company do |c| 
        = c.text_field :name 
        = end 
    
    +0

    이것은 나의 실수를 고쳤습니다. FYI ... Haml은 끝을 요구하지 않습니다. indentaion을 사용하여 블록을 결정합니다. 이것이 내 코드가 깨끗한 것처럼 느껴지는 이유입니다. – Herm

    +1

    오, 멋지다. 곧 살펴보기 시작할 것입니다. :) – luissimo