2017-04-20 20 views
0

ネストされたフォームを使用して、同時に2つのテーブルに保存しようとしています。なぜこれが起こっていないのか分かりません。 Railsのエラーは「1人のユーザーがこの人を救うことを禁じています:なぜ法人がPerson_idを取得していないのですか?ネストされたフォームの問題Rails

モデル

class Person < ApplicationRecord 
    has_one :legal_person, dependent: :destroy 
    accepts_nested_attributes_for :legal_person 
end 


class LegalPerson < ApplicationRecord 
    belongs_to :person 
end 

コントローラ

class PeopleController < ApplicationController 
    before_action :set_person, only: [:show, :edit, :update, :destroy] 

    # GET /people 
    # GET /people.json 
    def index 
    @people = Person.all 
    end 

    # GET /people/1 
    # GET /people/1.json 
    def show 
    end 

    # GET /people/new 
    def new 
    @person = Person.new 
    @person.build_legal_person 
    end 

    # GET /people/1/edit 
    def edit 
    end 

    # POST /people 
    # POST /people.json 
    def create 
    @person = Person.new(person_params) 

    respond_to do |format| 
     if @person.save 
     format.html { redirect_to @person, notice: 'Person was successfully created.' } 
     format.json { render :show, status: :created, location: @person } 
     else 
     format.html { render :new } 
     format.json { render json: @person.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /people/1 
    # PATCH/PUT /people/1.json 
    def update 
    respond_to do |format| 
     if @person.update(person_params) 
     format.html { redirect_to @person, notice: 'Person was successfully updated.' } 
     format.json { render :show, status: :ok, location: @person } 
     else 
     format.html { render :edit } 
     format.json { render json: @person.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /people/1 
    # DELETE /people/1.json 
    def destroy 
    @person.destroy 
    respond_to do |format| 
     format.html { redirect_to people_url, notice: 'Person was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_person 
     @person = Person.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def person_params 
     #params.require(:person).permit(:nome, :tipopessoa, :telefonefixo, :telefonecelular) 
     params.require(:person).permit(:nome, :tipopessoa, :telefonefixo, :telefonecelular, 
            legal_person_attributes: LegalPerson.attribute_names.map(&:to_sym)) 
            #legal_person_attributes: [:id, :cnpj, :nomefantasia, :person_id]) 
    end 
end 

エラー

Parameters: {"utf8"=>"✓", "authenticity_token"=>"pAAOYapSgnF8H6O2cJMpHSN6POBwAzycy00Tij5TKFDv+8d+EErtdQGKuRbay4IxKrUQjXVPq5MblVgjv5Fa5g==", "person"=>{"nome"=>"qweqw", "tipopessoa"=>"wqeqweq", "telefonefixo"=>"qweqe", "telefonecelular"=>"qweqw", "legal_person_attributes"=>{"cnpj"=>"qweqw", "nomefantasia"=>"wqewe"}}, "commit"=>"Criar Person"} 
    (0.1ms) BEGIN 
    (0.2ms) ROLLBACK 

答えて

0

使用は[:cnpj, :nomefantasia]代わり

LegalPerson.attribute_names.map(&:to_sym) 
の属性名0
関連する問題