文件上传 - 无法将哈希转换为整数

时间:2010-11-13 15:30:18

标签: ruby-on-rails-3 paperclip

我在linux上的rails 3.0.1上使用paperclip实现了一个带图像上传的表单 (在Windows上的rails 3.0.0上面的相同问题)

无论我在表单中输入什么,我都会得到类型错误“无法将哈希转换为整数”。 奇怪的是它工作了1次(表单提交+文件上传)。我可能在代码中的某处改变了某些东西,它再也没有用过。 (我无法想象我错在哪里)

查看:

<% form_for(@account,:url=>{ :action=>'create' },:html => { :multipart => true }) do |f| %>
  <%= render :partial => 'form', :locals => { :f => f } %>
  <%= f.submit %>
<% end %>

_form:

<%= f.error_messages %>
<%= f.text_field :company_name %><br />
<%= f.text_field :code %><br />
<%= f.text_field :website  %><br />
<%= f.file_field :logo  %><br />
<%= f.collection_select('industry_id', @industries, :id, :label, :include_blank=>true) %>

控制器:

def new
    @industries = Industry.find(:all, :order => 'label')
    @account = Account.new
end

def create
    @account = Account.new(params[:account])
    @account.begin_date = DateTime.now
    @account.active = 1
    if @account.save #LINE 247: 
      flash[:notice] = 'Account was successfully created.'
      redirect_to :action => 'show', :id => @account.id
    else
      render :action => 'new'
    end
  end

模型:

  has_attached_file :logo,
    :styles => {
    :thumb=> "100x100#",
    :small  => "150x150>",
    :medium => "300x300>",
    :large =>   "400x400>" },
    :url  => "/logos/:id/:style/:basename.:extension",
    :path => ":rails_root/public/logos/:id/:style/:basename.:extension"

LOG:

Started POST "/accounts/create" for 127.0.0.1 at 2010-11-13 10:08:27 +0800
[1m[36mSQL (31.2ms)[0m  [1mdescribe `groups_users`[0m
Processing by AccountsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ihg8Il2eHnMj/q9KmZPzOSB8W7NDs3hbpkEJ+WSHd10=", "account"=>{"company_name"=>"aa and co", "code"=>"aaco", "website"=>"aa.com", "logo"=>#<File:/usr/tmp/RackMultipart20101113-3624-oowopi>, "industry_id"=>"1"}, "commit"=>"Create Account"}
[1m[35mUser Load (0.0ms)[0m  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 1) LIMIT 1
[1m[36mUserType Load (0.0ms)[0m  [1mSELECT `user_types`.* FROM `user_types` WHERE (`user_types`.`id` = 1) LIMIT 1[0m
[paperclip] identify -format %wx%h "/usr/tmp/stream20101113-3624-s1ybvo.jpg[0]" 2>NUL
[paperclip] convert "/usr/tmp/stream20101113-3624-s1ybvo.jpg[0]" -resize "100x" -crop "100x100+0+2" +repage "/usr/tmp/stream20101113-3624-s1ybvo20101113-3624-7z7f43" 2>NUL
[paperclip] identify -format %wx%h "/usr/tmp/stream20101113-3624-s1ybvo.jpg[0]" 2>NUL
[paperclip] convert "/usr/tmp/stream20101113-3624-s1ybvo.jpg[0]" -resize "150x150>" "/usr/tmp/stream20101113-3624-s1ybvo20101113-3624-124zkow" 2>NUL
[paperclip] identify -format %wx%h "/usr/tmp/stream20101113-3624-s1ybvo.jpg[0]" 2>NUL
[paperclip] convert "/usr/tmp/stream20101113-3624-s1ybvo.jpg[0]" -resize "300x300>" "/usr/tmp/stream20101113-3624-s1ybvo20101113-3624-1jd454b" 2>NUL
[paperclip] identify -format %wx%h "/usr/tmp/stream20101113-3624-s1ybvo.jpg[0]" 2>NUL
[paperclip] convert "/usr/tmp/stream20101113-3624-s1ybvo.jpg[0]" -resize "400x400>" "/usr/tmp/stream20101113-3624-s1ybvo20101113-3624-w8mrk8" 2>NUL
[1m[35mSQL (0.0ms)[0m  BEGIN
[1m[36mSQL (0.0ms)[0m  [1mROLLBACK[0m Completed   in 1547ms

TypeError (can't convert Hash into Integer):
app/controllers/accounts_controller.rb:247:in `create'

我猜这个错误来自于表单,但我无法想象在哪里。

任何帮助都会非常感激。


最奇怪的是这种形式工作了两次: 昨天早上,刚启动计算机后,我启动了应用程序,提交了表单并且工作正常。后来我根本没有设法让它再次起作用(我没有改变任何东西)

今天早上,我启动了计算机并可以提交表格(当时没有试过图像)。再试一次,但失败了。重启计算机是没用的。

我现在正在做的是从表单中删除除1个字段以外的所有字段,以及来自控制器的任何代码,除了:@ account.save,但我仍然得到相同的错误,忽略我的模型中的验证...

我也试过在视图中直接硬编码的html表单(不使用form_for或form_tag),我得到了相同的结果。因此,可以安全地假设问题无法从控制器或视图中得到证实......(并且它也不是回形针问题)

1 个答案:

答案 0 :(得分:0)

问题已解决!两天与没有错误的代码打架...... :(

错误来自我模型中的验证!

validates_length_of :code, :is=>9, :message=>"has %d chars"

它工作的唯一时间可能输入9个字符并且验证不必显示%d消息...