1

使用回形針上傳文件。我有項目,附件和關聯表。我有一切可以上傳文件的地方,但是當它進入項目控制器時,它會尋找一個方法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

回答

0

我認爲這個問題是在附件模型:

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

嘗試

validates_attachment_presence :user_file