0

我正在尝试实现 Carrierware 和 nested_form 以便为我的模型获取多个上传,但遇到了麻烦。

我的模型:

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!'

此外,在视图中,当我单击“添加附件”时,会显示两个上传框而不是一个。谁能帮我弄清楚我的问题?

更新我的旧错误:

uninitialized constant Idea::SuggestedFilesByOwner

我需要添加:class_name => 'Attachment'到关联行。看上面

4

1 回答 1

1

undefined method 'file_will_change!'如果您忘记在模型的 db 表中添加列,则会发生。您必须将file列添加到Attachment,而不是Idea,因为必须将此列添加到具有上传者的模型中:

rails g migration AddFileToAttachments file:string
rake db:migrate
于 2013-06-27T09:12:28.487 回答