我在我的更新表單中預覽了附加圖像。當用戶在附件字段上獲取驗證錯誤時,會出現問題。在這種情況下,圖片縮略圖網址會變成好像圖片上傳時沒有任何錯誤(它顯示未在服務器上保存的文件的名稱)。驗證錯誤上的回形針錯誤附件網址
下面是我如何在我的視圖中獲取圖像URL: <%= image_tag(@product.photo.url(:medium)) %>
。
控制器:
def update
@product = Product.find(params[:id])
@product.update_attributes(params[:product]) ? redirect_to('/admin') : render(:new)
end
def edit
@product = Product.find(params[:id])
render :new
end
型號:
class Product < ActiveRecord::Base
<...>
@@image_sizes = {:big => '500x500>', :medium => '200x200>', :thumb=> '100x100>'}
has_attached_file :photo, :styles => @@image_sizes, :whiny => false
validates_attachment_presence :photo
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'], :message => I18n.t(:invalid_image_type)
validates_attachment_size :photo, :less_than => 1.megabytes, :message => I18n.t(:invalid_image_size, :max => '1 Mb')
after_post_process :save_image_dimensions
<...>
end
UPD:的simpliest溶液是在控制器的更新動作在視圖@product.update_attributes
和<% @photo_file_url ||= @product.photo.url(:medium) %>
之前添加@photo_file_url = @product.photo.url(:medium)
。
請附上您的代碼你的'edit'和'update'動作。 – raidfive 2011-04-02 23:51:26
他們很簡單。只有少數CRUD提示('Product.find(params [:id])'和'@ product.update_attributes(params [:product])'分別用於編輯和更新)。 – sunki 2011-04-02 23:56:18
使用這些更新編輯現有帖子是一種更常見的做法。 – raidfive 2011-04-02 23:58:33