ActiveRecord::AssociationTypeMismatch(动态图片上传器)

发布于 2024-11-24 14:04:45 字数 1639 浏览 2 评论 0原文

我正在尝试按照教程动态多图像使用 Ruby on Rails 上传,为照片创建一个新模型,并将它们与另一个模型关联起来。复制并粘贴所有代码后,我收到“AssociationTypeMismatch”错误,如下所示。

 ActiveRecord::AssociationTypeMismatch in AdminForksController#update
 Photo(#2176152540) expected, got Array(#2148417160)
 app/controllers/admin_forks_controller.rb:23:in `update'
 {   "commit"=>"update",
     "fork"=>{"position"=>"",
              "name"=>"FORK",
              "brand"=>"",
               "photos"=>{"data"=>#<ActionDispatch::Http::UploadedFile:0x103634328 @headers="Content-
               Disposition: form-data; name=\"fork[photos][data]\"; filename=\"marty.jpg\"\r\nContent-Type:
               image/jpeg\r\n", @tempfile=#<File:/var/folders/cZ/cZVO8X55FeynZw5cHRz1UE+++TI
               /-Tmp/RackMultipart20110716-18721-16ttjsd-0>, @content_type="image/jpeg",
               @original_filename="marty.jpg">},
     "authenticity_token"=>"iSzZxyzTe/LDLIf4cQiYBGLIk96INnKCP3SC5b5MXHw=",
     "utf8"=>"?",
     "id"=>"7"}

我的 forks 模型如下所示:

class Fork < ActiveRecord::Base
    has_many :photos
    accepts_nested_attributes_for :photos, :allow_destroy => true
end

我的照片模型如下所示:

class Photo < ActiveRecord::Base
    belongs_to :fork
    has_attached_file :data
end

看起来 :photos 作为数组传递,而 :fork 正在等待它。我该如何解决这个问题?

更新

问题已解决(请参阅答案)。但是,:fork@fork 这两个变量有何不同?

I am trying to follow the tutorial Dynamic multiple image uploads with Ruby on Rails that creates a new model for photos and will associate them with another model. After copying and pasting all the code I'm getting an "AssociationTypeMismatch" error as follows.

 ActiveRecord::AssociationTypeMismatch in AdminForksController#update
 Photo(#2176152540) expected, got Array(#2148417160)
 app/controllers/admin_forks_controller.rb:23:in `update'
 {   "commit"=>"update",
     "fork"=>{"position"=>"",
              "name"=>"FORK",
              "brand"=>"",
               "photos"=>{"data"=>#<ActionDispatch::Http::UploadedFile:0x103634328 @headers="Content-
               Disposition: form-data; name=\"fork[photos][data]\"; filename=\"marty.jpg\"\r\nContent-Type:
               image/jpeg\r\n", @tempfile=#<File:/var/folders/cZ/cZVO8X55FeynZw5cHRz1UE+++TI
               /-Tmp/RackMultipart20110716-18721-16ttjsd-0>, @content_type="image/jpeg",
               @original_filename="marty.jpg">},
     "authenticity_token"=>"iSzZxyzTe/LDLIf4cQiYBGLIk96INnKCP3SC5b5MXHw=",
     "utf8"=>"?",
     "id"=>"7"}

My forks model looks like this:

class Fork < ActiveRecord::Base
    has_many :photos
    accepts_nested_attributes_for :photos, :allow_destroy => true
end

And my photos model looks like this:

class Photo < ActiveRecord::Base
    belongs_to :fork
    has_attached_file :data
end

It looks like :photos is getting passed as an array, and :fork is expecting it. How do I resolve this issue?

Update

The issue was resolved (see the answer). However, how do the two variables, :fork and @fork, differ?

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

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

发布评论

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

评论(1

街道布景 2024-12-01 14:04:45

问题解决了。在我的 for_for 中,我使用 :fork 而不是 @fork

The problem was solved. In my for_for I was using :fork instead of @fork.

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