2013-03-11 21 views
1

我正在研究稱爲章節的模型,該模型允許用戶使用載波上傳多個圖像(+單獨的縮略圖圖像)。我創建了一個名爲Attachment的多態模型,用於裝載上傳者。Rails carrierwave多態附件 - 未定義的方法'image will_change!'錯誤

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 

附件

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 

我試着寫在上面的代碼後,提交表單,但我最終得到了「未定義的方法`chapter_image_will_change!」爲...「錯誤。經過一番搜索之後,我在另一篇文章中看到我需要運行一些遷移來擺脫錯誤。所以我做了以下。

rails g migration AddAttachmentToChapters 
    chapter_image:string chapter_thumb:string 

bundle exec rake db:migrate 

但是錯誤仍然存​​在。我會包含我的查看頁面代碼,以防它有用。

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> 

任何意見/幫助歡迎!

回答

3

您需要缺少的列添加到Attachment模式,而不是Chapter

rails g migration AddImageToAttachments chapter_image:string chapter_thumb:string 
+1

你是我的救命恩人。 – jeebface 2013-03-11 06:08:17

相關問題