gre*_*ide 6 ruby-on-rails carrierwave
在我的rails项目中,我使用Carrierwave通过雾将图像上传到S3.到目前为止,我有CRUD频谱的创建读取和删除部分工作.
我的问题是编辑/更新部分.我使用相同_form.html.erb的编辑我用于创建记录.当我单击编辑链接时,表单会将所有数据加载到表单字段中以进行编辑,但图像除外.表单字段为空白,好像没有与记录关联的图像.
如何更新已保存到S3的图像?
型号/ listing.rb
class Listing < ActiveRecord::Base
attr_accessible :body, :price, :title, :image
mount_uploader :image, ListingUploader
end
Run Code Online (Sandbox Code Playgroud)
Controllers/listings_controller.rb(编辑/更新部分)
def edit
@listing = Listing.find(params[:id])
end
def update
@listing = Listing.find(params[:id])
if @listing.update_attributes(params [:listing])
redirect_to :action => 'show', :id => @listing
else
render :action => 'edit'
end
end
Run Code Online (Sandbox Code Playgroud)
_form.html.erb
<%= form_for @listing, :html => { :multipart => true } do |l| %>
<p>
<%= l.label :title %>
<%= l.text_field :title %>
</p>
<p>
<%= l.label :price %>
<%= l.text_field :price %>
</p>
<p>
<label>Upload a Picture</label>
<%= l.file_field :image %>
<%= l.hidden_field :image_cache %>
</p>
<div class="image-pre">
<%= image_tag(@listing.image_url(:thumb)) if @listing.image? %>
</div>
<p>
<%= l.label :body %>
<%= l.text_area :body, :class => "tinymce" %>
<%= tinymce %>
</p>
<%= l.submit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
在您的listings_controller.rb中,尝试以下操作:
def edit
@listing = Listing.find(params[:id])
@listing.image.cache!
end
Run Code Online (Sandbox Code Playgroud)
我自己还没有测试过这一点,但我认为它可能会起作用,因为 image_cache 字段通常用于规避这个问题。
| 归档时间: |
|
| 查看次数: |
2783 次 |
| 最近记录: |