我正在创建一个简单的图像裁剪方法,并不断得到这个错误:符号到整数的隐式转换
控制器:
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
请求信息
Request parameters
{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"bpZyvToWkRoiXxivoiTeOh7TCLBvHbmLJCzBTs/zUN8=", "user"=>
{"useravatar_attributes"=>{"crop_x"=>"0", "crop_y"=>"0", "crop_w"=>"508", "crop_h"=>"508",
"id"=>"60"}}, "commit"=>"Crop", "action"=>"crop", "controller"=>"users", "id"=>"6"}
crop.html。erb
<%= 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 %>
似乎所有正确的数据都被传递了,有人理解这个错误吗?
编辑1:我使用了这个教程http://odysseyonrails.com/articles/10并做了一些更改。我没有使用单个类来上传,而是创建了多态STI关系类:
Asset.rb
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
useravatar.rb
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
看起来您的u变量不期望符号。只要删除键变量,因为您没有使用它,并尝试这样做:
params[:user][:useravatar_attributes].each do |u|
# your code goes here
end