我可以使用 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
几乎相似的代码在“创建”中工作得很好,但在“更新”操作中已经有一个值,这个代码不起作用。
我收到以下错误消息: Completed 500 Internal Server Error in 1089ms ActionView::Template::Error (undefined method `public_filename' for nil:NilClass): 显然这个错误出现在索引操作中,它试图列出所有记录和他们的附件。由于更新后这个附件是空的,所以在redirect_to部分会抛出这个错误。
我正在使用 REE1.8.7 和 rails 3.2.9
请帮忙!