ActiveRecord :: AssociationTypeMismatch(动态图像上传器) [英] ActiveRecord::AssociationTypeMismatch (dynamic image uploader)

查看:96
本文介绍了ActiveRecord :: AssociationTypeMismatch(动态图像上传器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循教程 使用Ruby on Rails动态上传多张图片 ,可为照片创建一个新模型并将其与另一个模型相关联.复制并粘贴所有代码后,出现如下"AssociationTypeMismatch"错误.

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"}

我的叉子模型如下:

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期望它.我该如何解决这个问题?

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

更新

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

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

推荐答案

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

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

这篇关于ActiveRecord :: AssociationTypeMismatch(动态图像上传器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆