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 を使用しています
助けてください!