Rails 4:一种形式的多个模型

时间:2015-11-19 14:04:06

标签: ruby-on-rails-4

我有一个更新Person属性的表单。 在相同的表单中,我可以编辑人员属性和地址属性。 但是地址属性不会被保存。 我错过了什么?

我的people_controller.rb:

class PeopleController < ApplicationController
  respond_to :html

  before_action :set_person, only: [:edit, :update, :destroy]

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

  # GET /people/1/edit
  def edit
  end

  # POST /people
  def create
    @person = Person.new(person_params)
    @address = @person.addresses.build(address_params)

    respond_to do |format|
      if @person.save || @address.save
        flash.now[:success] = I18n.t('notices.person_save_success')
        format.html { render :edit }
      else
        flash.now[:danger] = I18n.t('notices.person_save_failure')
        format.html { render :edit }
      end
    end
  end

  # PATCH/PUT /people/1
  def update
    respond_to do |format|
      if @person.update(person_params) || @address.update(address_params)
        flash.now[:success] = I18n.t('notices.person_update_success')
        format.html { render :edit }
      else
        flash.now[:danger] = I18n.t('notices.person_update_failure')
        format.html { render :edit }
      end
    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(:firstname, :lastname, :salutation, :title, :day_of_birth, :day_of_death,
        :recipient_catalog_general, :recipient_catalog_kids, :recipient_catalog_bavaria, :recipient_newsletter,
        :liable_for_sales_tax, :internal_annotation, :finances_iban, :finances_bic, :finances_bank,
        :finances_trade_id, :finances_tax_id)
    end

    def address_params
      params.require(:address).permit(:street, :zip, :lockbox, :city, :country, :user_id)
    end

end

在我的模特/ person.rb

 class Person < ActiveRecord::Base
      has_many :addresses, dependent: :destroy          
      accepts_nested_attributes_for :addresses
 end
模型/ address.rb中的

class Address < ActiveRecord::Base
  belongs_to :person
end
我的迁移中的

/ [timestamp] _create_addresses.rb:

class CreateAddresses < ActiveRecord::Migration
  def change
    create_table :addresses do |t|
      t.string :street
      t.string :zip
      t.string :city
      t.string :country
      t.string :lockbox
      t.string :type
      t.references :person, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

我的形式是haml:

= form_for @person do |f|
  .container-fluid
    .row
      .col-lg-6  
        # input fields for person attributes omitted
        = fields_for :address do |address|
          .row
            .col-lg-8
              = address.label :street, class: ''
              = address.text_field :street, class:'form-control'
          .row
            .col-lg-2
              = address.label :zip, class: ''
              = address.text_field :zip, class: 'form-control'
            .col-lg-6
              = address.label :city, class: ''
              = address.text_field :city, class: 'form-control'
          .row
            .col-lg-2
              = address.label :lockbox, class: ''
              = address.text_field :lockbox, class: 'form-control'
            .col-lg-6
              = address.label :country, class: ''
              = address.text_field :country, class: 'form-control'

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案:

在我的people_controller.rb中,我不得不将代码更改为:

@person = Person.new(person_params)
@address = @person.addresses.new(address_params)