在我的付款页面上,存在某些变量,例如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散列吗? –