我试图在保存模型之前操作嵌套参数,但我错过了一些东西

发布于 2024-08-28 15:58:19 字数 511 浏览 3 评论 0原文

我看不到我错过了什么。我有嵌套项目的订单,这些项目每个都有一种。我想操作每个 Item 的 kind_id 参数,但“f[:kind_id]”总是返回 0。

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

我得到的参数是

{"authenticity_token"=>"7wz7ARjwcVvCR/bpp/T04JQIQwHsMKDflF1eMCL8PTU=",
 "order"=>{"items_attributes"=>{"1271160144889"=>{"price"=>"2",
 "amount"=>"2",
 "text"=>"2",
 "kind_id"=>"fds",
 "_destroy"=>""}},
 "total_price"=>"4"}}

上面的参数当然是测试数据:)

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"=>"2",
 "text"=>"2",
 "kind_id"=>"fds",
 "_destroy"=>""}},
 "total_price"=>"4"}}

The above params is of course test data :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请叫√我孤独 2024-09-04 15:58:19

由于 :kind_id 是一个整数列,ActiveRecord 会自动将其解释为整数 ("fds".to_i #=> 0)。您应该将 attr_accessor :kind_name 添加到 Item 模型,并将表单字段切换为 kind_name。然后你可以做

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文