Ruby on rails 无需将符号隐式转换为整数轨道裁剪图像

Ruby on rails 无需将符号隐式转换为整数轨道裁剪图像,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我正在创建一种简单的图像裁剪方法,并不断出现以下错误: 无符号到整数的隐式转换 控制器: def crop @user = User.find(params[:id]) respond_to do |format| if @user.update_attributes(params[:user]) params[:user][:useravatar_attributes].each do |key, u| if(u[:crop_x] &a

我正在创建一种简单的图像裁剪方法,并不断出现以下错误: 无符号到整数的隐式转换

控制器:

def crop
@user = User.find(params[:id])
    respond_to do |format|
      if @user.update_attributes(params[:user])

        params[:user][:useravatar_attributes].each do |key, u|
          if(u[:crop_x] && u[:crop_y] && u[:crop_w] && u[:crop_h] && u[:id]) **ERROR AT THIS LINE**
            old_upload = Asset.find(u[:id].gsub(/\D/, '').to_i)
            if(old_upload)
              if(old_upload.update_attributes(u))
                old_upload.reprocess_pic
              end
            end
          end
        end
        format.html { redirect_to @user, notice: 'Card was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
请求信息

crop.html.erb

似乎所有正确的数据都被传递了,有人理解这个错误吗

编辑1:我使用了本教程并做了一些更改。我创建了多态性STI关系类,而不是使用单个类进行上传:

Asset.rb

useravatar.rb

看起来您的u变量不需要符号。只需删除键变量,因为您不使用它,然后尝试以下操作:

params[:user][:useravatar_attributes].each do |u|
  # your code goes here
end
<%= form_for @user, :url=> crop_user_path, :html => { :multipart => true } do |f| %>
    <%= f.fields_for :useravatar do |u| %>
        <% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>

            <%= u.text_field attribute, :id => "#{attribute}" %>
        <% end %>
    <% end %>
    <p><%= f.submit "Crop" %></p>
<% end %>
class Asset < ActiveRecord::Base
  attr_accessible :attachment_content_type, :attachment_file_name, :attachment_file_size, :attachment,
  :crop_x, :crop_y, :crop_w, :crop_h
  attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
  belongs_to :assetable, :polymorphic => true
  delegate :url, :to => :attachment

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

  def pic_geometry(style = :original)
    @geometry ||= {}
    @geometry[style] ||= Paperclip::Geometry.from_file(attachment.path(style))
  end

  def reprocess_pic
    attachment.reprocess!
  end
end
class Group::Useravatar < Asset
  has_attached_file :attachment,
  :styles => {
  :tiny => "36x36#",
  :thumb=> "60x60#",
  :feed=> "110x120#",
  :small  => "136x136#",
  :medium => "243x143>",
  :large => "786x500"},
  :default_url => "./images/default_avatar_:style.jpg",
  :processors => [:cropper]
  validates_attachment_content_type :attachment, 
  :content_type => ['image/jpeg', 'image/png']
  attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
attr_accessible :attachment_content_type, :attachment_file_name, :attachment_file_size, :attachment,
  :crop_x, :crop_y, :crop_w, :crop_h

end
params[:user][:useravatar_attributes].each do |u|
  # your code goes here
end