ActiveRecord::AssociationTypeMismatch(动态图片上传器)
我正在尝试按照教程动态多图像使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了。在我的
for_for
中,我使用:fork
而不是@fork
。The problem was solved. In my
for_for
I was using:fork
instead of@fork
.