我在下面有示例参数:
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"xxxxxxxxxx",
"post" => {
"product_attributes" => {
"name"=>"Ruby",
"product_dtls_attributes" => {
"0"=>{"price"=>"12,333.00"},
"1"=>{"price"=>"111,111.00"}
},
},
"content"=>"Some contents here."
}
现在,情况是,我无法获得中的price
确切值model
。
代替:
我得到:
现在,这就是我在代码中所做的事情:
before_validation(on: :create) do
puts "price = #{self.price}" # I also tried self.price.to_s, but didn't work.
end
更新:
(我正在尝试这样做是为了获得full value
并去除逗号)。
before_validation(on: :create) do
puts "price = #{self.price.delete(',').to_f}" # I also tried self.price.to_s, but didn't work.
end
笔记:
列price
是浮动的
问题是,我怎样才能获得params的确切值price
。谢谢!
我提出的解决方案是在控制器上处理它。迭代哈希,然后将其保存。然后它得到我想要获得的适当值并保存适当的值。
迭代以下哈希并保存。
"post" => {
"product_attributes" => {
"name"=>"Ruby",
"product_dtls_attributes" => {
"0"=>{"price"=>"12,333.00"},
"1"=>{"price"=>"111,111.00"}
},
},
"content"=>"Some contents here."
price
由于逗号分隔,我无法获得in模型的全部价值。这个逗号分隔符和小数点+小数位是由gem制成的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句