回形针和嵌套属性问题

时间:2011-03-28 23:35:19

标签: ruby-on-rails ruby-on-rails-3 associations paperclip nested-attributes

使用回形针上传文件。我有项目,附件和关联表。我有一切可以上传文件,但是当它进入项目控制器时,它会查找方法attachment_file_name,但是,我认为它应该是user_file_file_name,因为{{1附件模型用于:user_file语句的是什么。这是生产日志:

has_attachment

如果这有帮助,以下是模型:

Started POST "/projects" for 136.152.181.137 at Mon Mar 28 16:23:28 -0700 2011
  Processing by ProjectsController#create as HTML
  Parameters: {"commit"=>"Create Project", "authenticity_token"=>"xh0Ld2RKyJ6EF9jLNl3D+r1m50lKv389NCIor3H4fag=", "utf8"=>"?~\~S", "project"=>{"name"=>"Upload Test", "icon_id"=>"1", "categories_attributes"=>{"0"=>{"name"=>"Category 1", "_destroy"=>"false"}}, "content"=>"skee", "description"=>"description", "attachments_attributes"=>{"0"=>{"name"=>"name", "user_file"=>#<ActionDispatch::Http::UploadedFile:0x2aaaaf6b0fd8 @tempfile=#<File:/tmp/RackMultipart20110328-18838-4wasuv-0>, @headers="Content-Disposition: form-data; name=\"project[attachments_attributes][0][user_file]\"; filename=\"resized_DSCF0229.JPG\"\r\nContent-Type: image/jpeg\r\n", @original_filename="resized_DSCF0229.JPG", @content_type="image/jpeg">}}}}
Completed   in 6ms

NoMethodError (undefined method `attachment_file_name' for #<Attachment:0x2aaaaf6a0638>):
  app/controllers/projects_controller.rb:48:in `create'
  app/controllers/projects_controller.rb:47:in `create'

为什么要寻找那种方法?在此先感谢您的帮助!

这是projects_controller.rb创建方法:

class Project < ActiveRecord::Base
  has_many :project_file_assocs
  has_many :attachments, :through => :project_file_assocs
  accepts_nested_attributes_for :attachments
  attr_accessible :attachments_attributes
...
end


class Attachment < ActiveRecord::Base
  has_attached_file :user_file
  validates_attachment_presence :attachment

  has_many :project_file_assocs 
  has_many :projects, :through => :project_file_assocs
  attr_accessible :name, :description, :user_file_file_name, :user_file_content_type, :user_file_file_size
  #attr_accessible :name, :description, :user_file
end


class ProjectFileAssoc < ActiveRecord::Base
   belongs_to :attachment
   belongs_to :project
end

1 个答案:

答案 0 :(得分:0)

我认为问题出在附件模型中:

class Attachment < ActiveRecord::Base
  has_attached_file :user_file
  validates_attachment_presence :attachment

尝试

  validates_attachment_presence :user_file