2013-06-27 5 views
0

Я пытаюсь реализовать Carrierware и nested_form, чтобы получить несколько загрузок для моей модели, но у меня проблемы.Проблема с несущей и nested_forms

Моя модель:

class Idea < ActiveRecord::Base 
    attr_accessible :suggested_files_by_owner_attributes 
    has_many :suggested_files_by_owner, :class_name => 'Attachment', :as => :attachable 
    has_many :suggested_files, :class_name => 'Attachment', :as => :attachable 
    accepts_nested_attributes_for :suggested_files_by_owner 
end 

Моя модель:

class Attachment < ActiveRecord::Base 
    attr_accessible :title, :file 
    # belongs_to :comment 
    belongs_to :attachable, :polymorphic => true 
    mount_uploader :file, FileUploader 
end 

Мои схемы (я мигрировал):

create_table "attachments", :force => true do |t| 
    t.datetime "created_at",  :null => false 
    t.datetime "updated_at",  :null => false 
    t.string "title" 
    t.integer "attachable_id" 
    t.string "attachable_type" 

Я мигрировали:

class AddFileToIdeas < ActiveRecord::Migration 
    def change 
    add_column :ideas, :file, :string 
    end 
end 

Мой Вид:

= nested_form_for @idea, :url => {:action => "update", :controller=>"ideas"}, :html=>{:multipart => true} do |f| 
    =f.text_field :title, value: @idea.title 
    = f.text_area :description, :size => '30x7', :id => 'description' 
    %br 

    = f.label :privacy, "Make Private" 
    = f.check_box :is_private 
    %p 
    = f.fields_for :suggested_files_by_owner do |a_form| 
     = a_form.file_field :file 
     = a_form.link_to_remove "Remove" 
    = f.link_to_add "Add Attachment", :suggested_files_by_owner 


    = f.submit 'Save and Post' 

Моя ошибка возникает, когда я ударил представить:

undefined method `file_will_change!' 

Кроме того, на взгляд, когда я нажимаю «Добавить вложение», два Загрузчики коробки появляются вместо одного. Может ли кто-нибудь помочь мне разобраться в моих проблемах?

UPDATE Моя старая ошибка:

uninitialized constant Idea::SuggestedFilesByOwner 

мне нужно добавить :class_name => 'Attachment' в ассоциации линии. См. Выше

ответ

1

undefined method 'file_will_change!' возникает, если вы забыли добавить столбец в таблицу db модели. Вы должны добавить file столбец Attachment, не Idea, потому что этот столбец должен быть добавлен к модели, которая имеет загрузчик:

rails g migration AddFileToAttachments file:string 
rake db:migrate