File upload - can't convert hash into integer
I have implemented a form with image upload using paperclip on rails 3.0.1 on linux (same problem below on rails 3.0.0 on windows)
Whatever I enter in my form, I get the Type Error "can't convert hash into integer". The strange thing is that it worked 1 time (form submit + file upload). I probably changed something somewhere in the code and it never worked again. (I cannot figure where I wen wrong)
View:
<% 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) %>
controller:
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
model:
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]" -re开发者_高级运维size "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'
I'm guessing the error comes from the form, but I can't figure where.
Any help would greatly appreciated.
The strangest thing is that this form worked twice: Yesterday morning, just after starting the computer, I launched the app, submitted the form and it worked. Later I haven't managed to make it work again at all (and I haven't changed anything)
This morning, I started the computer and could submit the form (haven't tried with the image at that time). Trying again and it failed. Restarting the computer was useless.
What I am doing now is removing all but 1 field from the form, and any code from the controller except: @account.save, but i still get the same error, ignoring the validations in my model...
I also tried with a html form directly hardcoded in the view (not using form_for or form_tag) and i was getting the same result. So it's probably safe to assume the problem doesn't comme from the controller or the view... (and it's not a paperclip issue either)
Problem fixed! Two days of fighting with a code that wasn't at fault... :(
The error came from a validation in my model!
validates_length_of :code, :is=>9, :message=>"has %d chars"
The only times when it worked as was probably entering 9 characters and the validation didn't have to display the %d message...
精彩评论