0

複数の請求先住所と郵送先住所を持つ顧客プロファイルのネストされたグループを作成しようとしています。顧客プロファイル フォームを参照すると、顧客プロファイル フォームのフィールドは表示されますが、請求先住所フォームのフィールドは表示されません。郵送先住所フォーム。何か案は?私のコントローラー、モデル、ビューは以下です。

目標は、複数の請求先住所と複数の郵送先住所に対応できる 1 つの顧客プロファイルを持つことです。

顧客プロファイル モデル:

    class CustomerProfile < ActiveRecord::Base
        #These entries are required to create a nested model form (multiple models in one form)
        validates_presence_of :customerNumber
        validates_uniqueness_of :customerNumber


        has_many :billing_addresses, :dependent => :destroy
        has_many :postal_addresses,  :dependent => :destroy

        accepts_nested_attributes_for :billing_addresses, :reject_if => lambda { |a| a[:content].blank?}, :allow_destroy => true
        accepts_nested_attributes_for :postal_addresses, :reject_if => lambda { |a| a[:content].blank?}, :allow_destroy => true
    end

請求先住所モデル:

    class BillingAddress < ActiveRecord::Base
        belongs_to :customer_profile
        attr_protected :customerNumber
    end

住所モデル:

    class PostalAddress < ActiveRecord::Base
        belongs_to :customer_profile
        attr_protected :customerNumber
    end

顧客プロファイル コントローラー:

    def new
        @customer_profile = CustomerProfile.new

        respond_to do |format|
          format.html # new.html.erb
          format.json { render json: @customer_profile }
        end
    end


    def create
        @customer_profile = CustomerProfile.new(params[:customer_profile])

        respond_to do |format|
          if @customer_profile.save
            format.html { redirect_to @customer_profile, notice: 'Customer profile was successfully created.' }
            format.json { render json: @customer_profile, status: :created, location: @customer_profile }
          else
            format.html { render action: "new" }
            format.json { render json: @customer_profile.errors, status: :unprocessable_entity }
          end
        end
    end

顧客プロフィールフォーム:

    <br />
    <h3>Add new customer profile with Billing and Postal Address</h3>
    <br />
    <h4> Customer Profile </h4>
    <%= form_for @customer_profile do |f| %>
        <div class="field">
            <%= f.label :customerNumber %><br />
            <%= f.text_field :customerNumber %>
        </div>

        #.... a few other fields removed to keep this short

    <br />
        <h4> Billing Address </h4>
        <%= f.fields_for :billing_addresses do |b| %>

            <div class="field">
                <%= b.label :addressLine1 %><br />
                <%= b.text_field :addressLine1 %>
            </div>

        #.... a few other fields removed to keep this short

        <% end %>

        <br />
        <h4> Postal Address </h4>
        <br />
        <%= f.fields_for :postal_addresses do |p| %>

            <div class="field">
                <%= p.label :addressLine1 %><br />
                <%= p.text_field :addressLine1 %>
            </div>

        #.... a few other fields removed to keep this short

        <br />
        <% end %>
        <%= f.submit %>
    <% end %>
4

1 に答える 1

0

fields_for には、customer_profile の子であるbilling_addresses のフィールドが表示されます。これが新しいオブジェクトである場合は、1 つ以上を追加するか、JavaScript を使用して適切なフィールドを追加する必要があります。両方にしたい場合があるので、そこに到達したときに空のフィールドがあり、後でさらに追加できます。

@customer_profile.billing_addresses.buildフォームがレンダリングされるときに空のものを持つためにa を行うことができます。

JavaScript を使用してフィールドを追加および削除する方法を知っていますか? レールキャストでよく取り上げられています。

于 2013-02-05T23:45:10.930 に答える