ActiveRecord::AssociationTypeMismatch (dynamic image uploader)

喜欢而已 提交于 2019-12-25 01:39:10

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/6718779/activerecordassociationtypemismatch-dynamic-image-uploader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!