开发者

I am trying to manipulate nested parameter before saving the model, but i miss something

I cant see what im missing. I have and Order with nested Items, these Items each have a Kind. I want to manipulate the kind_id param from each Item but the "f[:kind_id]" always return 0.

@order.items.each do |f|
  f[:kind_id] =  Kind.find_by_name(f[:kind_id]).id
end

the params i get is

{"authenticity_token"=>"7wz7ARjwcVvCR/bpp/T04JQIQwHsMKDflF1eMCL8PTU=",
 "order"=>{"items_attributes"=>{"1271160144889"=>{"price"=>"2",
 "amount"=&开发者_JAVA百科gt;"2",
 "text"=>"2",
 "kind_id"=>"fds",
 "_destroy"=>""}},
 "total_price"=>"4"}}

The above params is of course test data :)


Because :kind_id is an integer column, ActiveRecord is automatically interpreting it as an integer for you ("fds".to_i #=> 0). You should add attr_accessor :kind_name to the Item model and switch the form field to kind_name. Then you can do

@order.items.each do |f|
  f.kind = Kind.find_by_name(f.kind_name)
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜