在我的付款頁面上,存在某些變量,例如card_number,我想從視圖傳遞給模型,但我不想將它們存儲在數據庫中。我通常可以很容易地通過簡單地使用attr_accessor實現這一點,但在這種情況下,模型是通過PARAMS被accepts_nested_attributes_for過去了,由於某些原因,PARAMS不被穿過:attr_accessor在accept_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
現在奇怪的是,當我嘗試保存一條記錄時,它只是返回一個空的異常。該參數似乎沒有通過accept_nested_attributes_for從用戶傳遞給CreditCard?
在傳遞的參數是:
{"email"=>"[email protected]", "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"}}}
有誰知道怎麼回事? accept_nested_attributes_for與attr_accessor一起使用嗎?
你有沒有在控制器試圖調試,看是否params哈希表有什麼你認爲它應該? –
嘿是的,我有,它的作品,如果我把領域的父母模型,並在那裏做。看起來,它只是不受accept_nested工作,但肯定如果這種情況下,其他人也會注意到這一點 – Nick
params哈希是什麼樣子?你可以發佈用戶控制器看到的params散列嗎? –