jeebface
jeebface

Reputation: 841

Rails carrierwave polymorphic attachment - undefined method 'image will_change!' error

I'm working on a model called Chapter that allows users to upload multiple images (+ a separate thumbnail image) using carrierwave. I've created a polymorphic model called Attachment that mounts the uploaders.

Chapter

class Chapter < ActiveRecord::Base
  attr_accessible :title, :uploader_comment, :book_id, :attachments_attributes

  has_many :comments, as: :commentable
  has_many :attachments, as: :attachable

  accepts_nested_attributes_for :attachments

  belongs_to :book
  belongs_to :user

  validates :title, presence: true, length: {maximum: 60}
  validates :book_id, presence: true
  validates :uploader_comment, length: {maximum: 150}
  validates :user_id, presence: true 
  validates :attachments, presence: true
end

Attachment

class Attachment < ActiveRecord::Base
    attr_accessible :chapter_image, :chapter_thumb

    belongs_to :attachable, polymorphic: true

    mount_uploader :chapter_image, ChapterImageUploader
    mount_uploader :chapter_thumb, ChapterThumbnailUploader

    validates_presence_of :chapter_image

end

I tried submitting the form after writing the above codes, but I ended up getting the "undefined method `chapter_image_will_change!' for..." error. After a little bit of searching, I saw on another post that I need to run a few migrations to get rid of the error. So I did the following.

rails g migration AddAttachmentToChapters 
  chapter_image:string chapter_thumb:string

bundle exec rake db:migrate

But the error still persists. I'll include my view page code in case it'll be helpful.

new.html.erb

<div class = "row">
  <div class = "span6 offset3">

    <%= simple_nested_form_for @chapter, html: {multipart: true}, 
        defaults: {required: false} do |f| %>

      <%= render 'shared/error_messages', object: @chapter %>

      <%= f.association :book, collection: current_user.books.all, 
                        tag: :book_id, include_blank: false %>

      <%= f.input :title, label: 'Chapter title' %>

      <%= f.input :uploader_comment %>

      <div class = "control-label">
        Image file upload
      </div>
      <%= f.simple_fields_for :attachments do |attachment_form| %>
        <%= attachment_form.file_field :chapter_image %>
        <%= attachment_form.link_to_remove 'Remove' %>
      <% end %>
      <%= f.link_to_add 'Add image', :attachments %>
      <span class="hint_end">Acceptable file formats: JPG, JPEG, GIF, PNG</span>

      <div class = "control-label">
        Thumbnail upload
      </div>
      <%= f.file_field :chapter_thumb %>
      <span class="hint_end">Acceptable file formats: JPG, JPEG, GIF, PNG</span>

      <%= f.submit "Upload chapter" %>

    <% end %>
  </div>
</div>

Any advice/help welcome!

Upvotes: 1

Views: 2274

Answers (1)

Ilia Khokhriakov
Ilia Khokhriakov

Reputation: 3687

You need to add missing columns to Attachment model, not Chapter:

rails g migration AddImageToAttachments chapter_image:string chapter_thumb:string

Upvotes: 3

Related Questions