回形针和嵌套属性问题

问题描述:

使用回形针上传文件。我有项目,附件和关联表。我有一切可以上传文件的地方,但是当它进入项目控制器时,它会寻找一个方法attachment_file_name,但是我相信它应该是user_file_file_name,因为: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' 

如果这会有所帮助,这里有型号:

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 

为什么会寻找该方法?预先感谢任何帮助!

这里是projects_controller.rb创建方法:

def create 
    @project = Project.new(params[:project]) 

    respond_to do |format| 
     if @project.save 
     format.html { redirect_to(@project, :notice => 'Project was successfully created.') } 
     format.xml { render :xml => @project, :status => :created, :location => @project } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @project.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 
+0

如果是这样的生产日志,它不会在开发环境中给出同样的错误吗? – YenTheFirst 2011-03-29 00:10:10

+0

我只使用生产 – pyramation 2011-04-02 01:35:47

我认为这个问题是在附件模型:

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

尝试

validates_attachment_presence :user_file