4

Rails 3.1rc4を使用して、フォーム内のネストされたモデルでいくつかの問題が発生しています。

私は現在、次のようなモデルを持っています:

class Sale < ActiveRecord::Base
  attr_accessible :customer_id, :vehicle_id, :sale_date

  belongs_to :customer
  accepts_nested_attributes_for :customer
end

class Customer < ActiveRecord::Base
  attr_accessible :dealership_id, :first_name, :last_name, :address1, :email

  belongs_to :dealership

  has_many :sales
  has_many :vehicles, :through => :sales

end

私は明らかにこれらをわずかに切り捨てましたが、すべての重要な情報がそこにあります。

私は、新しい顧客を作成することもできる販売フォームを設定しようとしています。したがって、accepts_nested_attributes_for :customer販売モデルのラインです。

私のフォームビューは次のようになります(ここでも切り捨てられ、重要な部分のみ):

    <%= form_for @sale, :html => {:class => 'fullform'} do |f| %>

        <%= f.error_messages %>

        <%= field_set_tag 'Customer Details' do %>
            <% f.fields_for :customer do |builder| %>
                <%= builder.label :first_name %><br>
                <%= builder.text_field :first_name %>
            <% end %>
        <% end %>
    <% end %>

私が抱えている問題は、フォームのレンダリング時にテキストフィールドも:first_nameのラベルも表示されないことです。エラーメッセージは表示されず、表示されないだけです。

コントローラー@sale.customer.buildの方法で試してみましたが、効果がなかったようです。new

ありがとう!

誰かが私が間違っていることを提案できますか?

編集:誤解を避けるために、私のセールスコントローラーの新しい方法は次のようになります:

def new
  @sale = Sale.new
  @sale.customer.build
end
4

1 に答える 1

4

モデルに追加customer_attributesします。attr_accessibleSale

別の間違い。交換:

<% f.fields_for :customer do |builder| %>

と:

<%= f.fields_for :customer do |builder| %>
于 2011-07-14T23:08:24.420 に答える