开发者

ActiveRecord::AssociationTypeMismatch (dynamic image uploader)

开发者 https://www.devze.com 2023-03-20 23:02 出处:网络
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 c

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 mod开发者_Go百科el 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消