I have a problem: I can't get :crop_x, :crop_y, :crop_w, :crop_h from html.erb file.
This is code in 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 %>
Controller/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
Model/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
In model, error happened at:
args = args.collect { |a| a.to_i }
It can't convert symbol to integer, but when I remove this line, img can't read these values to crop. I'm sure that 4 values is a integer.