0

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.

yagi_chan
  • 13
  • 6

2 Answers2

0

You're not using the symbols (:crop_x, :crop_y, ...) properly. I think your intent is to use the symbols as method names rather than just as symbols.

For your cropping, there's no need for symbols at all, just use the methods:

def cropping?
  !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end

Similarly, don't bother with symbols when building args, just call the accessor methods directly:

args = [ crop_x, crop_y, crop_w, crop_h ]

and you probably don't need the to_i calls at all since your cropping? method should keep nils away. If you really want to use symbols for some reason, then you'd hand the symbols to send to call the corresponding methods:

args = [ :crop_x, :crop_y, :crop_w, :crop_h ].map { |m| send(m) }

but there's really no need for that.

In the form:

<%= 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' %>

you use symbols because f.number_field wants a name so that it can build the HTML with the appropriate name attributes and so that it can call the methods to get any initial values.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • I can't still solve this problem. I did two your way: 1) Remove symbol: args = [ crop_x, crop_y, crop_w, crop_h ]. 2) Use this: args = [ :crop_x, :crop_y, :crop_w, :crop_h ].map { |m| send(m) } =>>> But both two ways make server run forever. Server can't stop except I shutdown it. What problems in my code? :( – yagi_chan Aug 28 '13 at 03:23
  • How are `crop_x`, `crop_y`, ... defined in your model? Do you know where the infinite loop happens? – mu is too short Aug 28 '13 at 03:49
  • I'm a newbie in ruby on rails. Crop_x, crop_y, ... are just a field in database and I didn't define it in model. I just set params in controller: params.require(:product).permit(:crop_x, :crop_y, :crop_w, :crop_h). What's wrong in my code? – yagi_chan Aug 28 '13 at 03:57
  • You don't need them in the database, you don't want those values to be persistent, just set them up with `attr_accessor`. – mu is too short Aug 28 '13 at 04:02
  • I tried it and it makes me some error. This is my post about that: http://stackoverflow.com/questions/18457342/error-when-using-attr-accessible-and-attr-accessor. Please help!!! – yagi_chan Aug 28 '13 at 04:06
0

I think you can't use the after_update callback.

The line self.attach.reprocess! will turn it into an infinite loop, because of the following reason:

Paperclip saves the current instance which has the image while reprocessing

Here is the code from Paperclip gem:

315  def reprocess!(*style_args)
316    saved_only_process, @options[:only_process] = @options[:only_process], style_args
317    begin
318      assign(self)
319      save
320      instance.save
321    rescue Errno::EACCES => e
322      warn "#{e} - skipping file."
323      false
324    ensure
325      @options[:only_process] = saved_only_process
326    end
327  end

As you can see, on line 320 from the above code it is saving the instance. This means it automatically runs the callbacks that's why you will get the infinite loop on server.

Please use this link.

Mischa
  • 42,876
  • 8
  • 99
  • 111
Kiran Kumara
  • 164
  • 5