我有一个问题:我无法从 html.erb 文件中获取 :crop_x, :crop_y, :crop_w, :crop_h。
这是crop.html.erb中的代码:
<%= image_tag @product.attach.url(:large), :id => "cropbox" %>
<h4>Preview:</h4>
<div style="width:100px; height:100px; overflow:hidden">
<%= image_tag @product.attach.url(:large), :id => "preview" %>
</div>
<%= form_for @product do |f| %>
<%= f.number_field :crop_x, :id => 'crop_x' %>
<%= f.number_field :crop_y, :id => 'crop_y' %>
<%= f.number_field :crop_w, :id => 'crop_w' %>
<%= f.number_field :crop_h, :id => 'crop_h' %>
<p><%= f.submit "Crop" %></p>
<% end %>
控制器/products_controller.rb:
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
# respond_to do |format|
if @product.update(product_params)
# redirect_to @product
if params[:product][:attach].blank?
flash[:notice] = "Successfully update user."
redirect_to @product
else
render :action => "crop"
end
# format.html { redirect_to @product, notice: 'Product was successfully updated.' }
# format.json { head :no_content }
else
render action: 'edit'
end
# end
end
型号/product.rb:
after_update :reprocess_avatar, :if => :cropping?
def cropping?
!:crop_x.blank? && !:crop_y.blank? && !:crop_w.blank? && !:crop_h.blank?
end
def reprocess_avatar
# product = Product.find(id)
img = Magick::ImageList.new(self.attach.path(:original))
args = [ :crop_x, :crop_y, :crop_w, :crop_h ]
args = args.collect { |a| a.to_i }
begin
img.crop!(*args)
img.write(self.attach.path(:original))
self.attach.reprocess!
self.save
rescue => ex
logger.error ex.message
end
# img.save!
end
在模型中,错误发生在:
args = args.collect { |a| a.to_i }
它无法将符号转换为整数,但是当我删除这一行时,img 无法读取这些值进行裁剪。我确定 4 个值是一个整数。