2013-02-12 99 views
0

我能够使用Attachment_fu将图片文件作为附件上传,但是当我尝试编辑/修改已经上传的图片时,它不起作用。 在我的控制器我有这样的:使用Attachment_fu更新文件附件(图片)问题

 def update 
      @sponsor_account = SponsorAccount.find(params[:id]) 
      if params[:showcase_category_image_file] &&   
      !params[:showcase_category_image_file].blank? 
      @sponsor_account.showcase_category_image = 
      ShowcaseCategoryImage.new(:uploaded_data => 
      params[:showcase_category_image_file]) 
     ***This logs - Now the file name is: *** 
      Rails.logger.info("Now the file name is: # 
      {@sponsor_account.showcase_category_image.filename}") 
      end 
     @sponsor_account.update_attributes(params[:sponsor_account]) 
    if @sponsor_account.save 
     flash[:notice] = "Showcase category #{@sponsor_account.name} was updated!" 
    else 
     flash[:errors] = @sponsor_account.errors 
    end 
     redirect_to sponsor_accounts_path 
    end 

ShowcaseCategoryImage定义如下:

has_attachment :content_type => :image, 
      :storage => :file_system, 
      :max_size => 5.megabytes, 
      :thumbnails => { :large => [350, 100], :medium => [200, 90], :thumb => 
       [35,35], :preview => [60,60] } 

    validates_as_attachment 

视图有一个file_field_tag如下:

 <%= file_field_tag :showcase_category_image_file%> 

和我SponsorAccount模型认为:

  has_one :showcase_category_image, :as => :owner, :dependent => :destroy 
     validates_presence_of :showcase_category_image, :on => :create 

几乎类似的代码在'create'中完全可以正常工作,但在'update'操作中已经有一个值,这段代码不起作用。

我提示以下错误:封邮件: 完成500内部服务器错误1089ms ::的ActionView ::模板错误(未定义的方法`public_filename”的零:NilClass): 这显然是错误的索引操作,其中它会尝试列出所有记录及其附件。由于此更新在更新后为空,因此此错误将在redirect_to部件中引发。

我正在使用REE1.8.7和rails 3.2.9

请帮忙!

回答

0

当我在'视图'中添加multipart => true时,此问题已解决。我正在使用rails 3.2.9,并且rails api有关于'file_field_tag'的说法:

file_field_tag(name,options = {})链接 创建文件上载字段。 如果您正在使用文件上传,则还需要为表单标记设置多部分选项: