无法从html.erb获取值

时间:2013-08-28 02:40:06

标签: ruby-on-rails ruby ruby-on-rails-4

我遇到了问题:我无法获取:crop_x,:crop_y,:crop_w,:来自html.erb文件的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个值是一个整数。

2 个答案:

答案 0 :(得分:0)

您没有正确使用这些符号(:crop_x:crop_y,...)。我认为你的意图是使用符号作为方法名称,而不仅仅是符号。

对于cropping,根本不需要符号,只需使用方法:

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

同样,在构建args时不要打扰符号,只需直接调用访问器方法:

args = [ crop_x, crop_y, crop_w, crop_h ]

并且您可能根本不需要to_i来电,因为您的cropping?方法应该保持nil秒。如果你真的想出于某种原因使用符号,那么你可以将符号交给send来调用相应的方法:

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

但实际上没有必要这样做。

表格形式:

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

您使用符号是因为f.number_field需要一个名称,以便它可以使用适当的name属性构建HTML,以便它可以调用方法来获取任何初始值。

答案 1 :(得分:0)

我认为您无法使用after_update回调。

由于以下原因,行self.attach.reprocess!会将其变为无限循环:

  
    

Paperclip在重新处理时保存当前具有图像的实例

  

Here是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

如您所见,在上面代码的第320行,它正在保存实例。这意味着它会自动运行回调,这就是为什么你会在服务器上获得无限循环的原因。

请使用this link

相关问题