質問

私の支払いページには、ビューからモデルに渡したいCard_Numberなどの特定の変数がありますが、DBに保存したくありません。私は通常、attr_accessorを使用するだけでこれを簡単に達成できますが、この場合、モデルはcomppods_nested_attributes_forを通じてパラメージで渡されており、何らかの理由でパラメーションは渡されません。

user.rbで私は持っています

  has_many :credit_cards

  accepts_nested_attributes_for :credit_cards

ビューファイルには、ネストされたフォームフィールドがあります。

  blah blah
  <h2>Credit card</h2>
  <%= f.fields_for :credit_cards do |builder| %>
    <%= render "credit_card_fields", :f => builder %>
  <% end %>

  inside that
  <p>
    <%= f.label :test %><br />
    <%= f.text_field :test %>
  </p>

credit_card.rbに戻って、私は持っています:

attr_accessor :test

before_create :show_me_test_param

private

def show_me_test_param
  raise "#{test}"
end

奇妙なことは、レコードを保存しようとすると、単に空の例外を返すということです。 PARAMは、Accepts_nested_attributes_forを通じてユーザーからクレジットカードに渡されたようには見えませんか?

渡されるパラメーションは次のとおりです。

{"email"=>"name@example.com", "password"=>"pass123", "password_confirmation"=>"pass123", "credit_cards_attributes"=>{"0"=>{"test"=>"helllo this is the second attempt", "name_on_card"=>"first lastname", "card_number"=>"987498742897", "card_verification_value"=>"232", "expiry_date"=>"2141"}}}

誰もが何が起こっているのか知っていますか? ATCOPTS_NESTED_ATTRIBUTES_FOR ATTR_ACCERSERで動作しますか?

役に立ちましたか?

解決

これは私を過去に何度か台無しにしました!ネストされたオブジェクトのパラメーションはキーを持ってコントローラーに来ます model_name_attributes コントローラー内のモデルのnewまたはupdate_attributesメソッドに渡されます。

追加する必要があります :credit_card_attributes あなたに attr_accessor そのキーを渡すことを許可します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top