多文件上载选项会产生错误轨道

时间:2013-03-26 21:07:18

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord ruby-on-rails-3.2

当我在视图中向:multiple => true添加选项f.file_field :picture,时,在提交表单时会出现can't convert nil into String错误。为什么会这样做,我该如何解决?

完整跟踪

carrierwave (0.8.0) lib/carrierwave/uploader/cache.rb:159:in `join'
carrierwave (0.8.0) lib/carrierwave/uploader/cache.rb:159:in `cache_path'
carrierwave (0.8.0) lib/carrierwave/uploader/cache.rb:131:in `cache!'
carrierwave (0.8.0) lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
carrierwave (0.8.0) lib/carrierwave/uploader/cache.rb:122:in `cache!'
carrierwave (0.8.0) lib/carrierwave/mount.rb:327:in `cache'
carrierwave (0.8.0) lib/carrierwave/mount.rb:179:in `picture='
carrierwave (0.8.0) lib/carrierwave/orm/activerecord.rb:38:in `picture='
activerecord (3.2.11) lib/active_record/attribute_assignment.rb:85:in `send'
activerecord (3.2.11) lib/active_record/attribute_assignment.rb:85:in `assign_attributes'
activerecord (3.2.11) lib/active_record/attribute_assignment.rb:78:in `each'
activerecord (3.2.11) lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
activerecord (3.2.11) lib/active_record/base.rb:497:in `initialize'
activerecord (3.2.11) lib/active_record/reflection.rb:183:in `new'
activerecord (3.2.11) lib/active_record/reflection.rb:183:in `build_association'
activerecord (3.2.11) lib/active_record/associations/association.rb:233:in `build_record'
activerecord (3.2.11) lib/active_record/associations/collection_association.rb:112:in `build'
activerecord (3.2.11) lib/active_record/associations/collection_proxy.rb:46:in `__send__'
activerecord (3.2.11) lib/active_record/associations/collection_proxy.rb:46:in `new'
app/controllers/admin/images_controller.rb:17:in `create'
actionpack (3.2.11) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.11) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.11) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.11) lib/abstract_controller/callbacks.rb:18:in `process_action'
activesupport (3.2.11) lib/active_support/callbacks.rb:425:in `_run__164786065__process_action__352915844__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `send'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `send'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `instrument'
activesupport (3.2.11) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.11) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.11) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.11) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.11) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.11) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.11) lib/action_controller/metal.rb:246:in `action'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:601:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.11) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `_run__1862416574__call__4__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `send'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `send'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.11) lib/rails/engine.rb:479:in `call'
railties (3.2.11) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
rack (1.4.5) lib/rack/handler/webrick.rb:13:in `run'
rack (1.4.5) lib/rack/server.rb:268:in `start'
railties (3.2.11) lib/rails/commands/server.rb:70:in `start'
railties (3.2.11) lib/rails/commands.rb:55
railties (3.2.11) lib/rails/commands.rb:50:in `tap'
railties (3.2.11) lib/rails/commands.rb:50
script/rails:6:in `require'
script/rails:6

完整错误

TypeError in Admin::ImagesController#create

can't convert nil into String
Rails.root: /Users/derek_duncan/Documents/Websites/Bud Beverly Photography/admin

Application Trace | Framework Trace | Full Trace
carrierwave (0.8.0) lib/carrierwave/uploader/cache.rb:159:in `join'
carrierwave (0.8.0) lib/carrierwave/uploader/cache.rb:159:in `cache_path'
carrierwave (0.8.0) lib/carrierwave/uploader/cache.rb:131:in `cache!'
carrierwave (0.8.0) lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
carrierwave (0.8.0) lib/carrierwave/uploader/cache.rb:122:in `cache!'
carrierwave (0.8.0) lib/carrierwave/mount.rb:327:in `cache'
carrierwave (0.8.0) lib/carrierwave/mount.rb:179:in `picture='
carrierwave (0.8.0) lib/carrierwave/orm/activerecord.rb:38:in `picture='
activerecord (3.2.11) lib/active_record/attribute_assignment.rb:85:in `send'
activerecord (3.2.11) lib/active_record/attribute_assignment.rb:85:in `assign_attributes'
activerecord (3.2.11) lib/active_record/attribute_assignment.rb:78:in `each'
activerecord (3.2.11) lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
activerecord (3.2.11) lib/active_record/base.rb:497:in `initialize'
activerecord (3.2.11) lib/active_record/reflection.rb:183:in `new'
activerecord (3.2.11) lib/active_record/reflection.rb:183:in `build_association'
activerecord (3.2.11) lib/active_record/associations/association.rb:233:in `build_record'
activerecord (3.2.11) lib/active_record/associations/collection_association.rb:112:in `build'
activerecord (3.2.11) lib/active_record/associations/collection_proxy.rb:46:in `__send__'
activerecord (3.2.11) lib/active_record/associations/collection_proxy.rb:46:in `new'
app/controllers/admin/images_controller.rb:17:in `create'
actionpack (3.2.11) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.11) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.11) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.11) lib/abstract_controller/callbacks.rb:18:in `process_action'
activesupport (3.2.11) lib/active_support/callbacks.rb:425:in `_run__164786065__process_action__352915844__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `send'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `send'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `instrument'
activesupport (3.2.11) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.11) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.11) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.11) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.11) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.11) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.11) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.11) lib/action_controller/metal.rb:246:in `action'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:601:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in `call'
activerecord (3.2.11) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `_run__1862416574__call__4__callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `send'
activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `send'
activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.11) lib/rails/engine.rb:479:in `call'
railties (3.2.11) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
rack (1.4.5) lib/rack/handler/webrick.rb:13:in `run'
rack (1.4.5) lib/rack/server.rb:268:in `start'
railties (3.2.11) lib/rails/commands/server.rb:70:in `start'
railties (3.2.11) lib/rails/commands.rb:55
railties (3.2.11) lib/rails/commands.rb:50:in `tap'
railties (3.2.11) lib/rails/commands.rb:50
script/rails:6:in `require'
script/rails:6
Request

Parameters:

{"image"=>{"picture"=>[#<ActionDispatch::Http::UploadedFile:0x10ee93248 @tempfile=#<File:/var/folders/bx/6z1z5yks56j40v15n43tjh1c0000gn/T/RackMultipart20130326-11396-8ebtao-0>,
 @headers="Content-Disposition: form-data; name=\"image[picture][]\"; filename=\"IMG_0106.JPG\"\r\nContent-Type: image/jpeg\r\n",
 @content_type="image/jpeg",
 @original_filename="IMG_0106.JPG">,
 #<ActionDispatch::Http::UploadedFile:0x10ee93220 @tempfile=#<File:/var/folders/bx/6z1z5yks56j40v15n43tjh1c0000gn/T/RackMultipart20130326-11396-1d1ltko-0>,
 @headers="Content-Disposition: form-data; name=\"image[picture][]\"; filename=\"Screen Shot 2013-03-06 at 4.16.27 PM.png\"\r\nContent-Type: image/png\r\n",
 @content_type="image/png",
 @original_filename="Screen Shot 2013-03-06 at 4.16.27 PM.png">],
 "description"=>"dfas",
 "album_id"=>"10",
 "albumcover"=>"false",
 "title"=>"dfsa",
 "image_id"=>"id"},
 "commit"=>"Create Image",
 "authenticity_token"=>"O9/iRhRJozks9ZE/LXfbDDZQa2/vEFcigu7Edkn3bWY=",
 "album_id"=>"10",
 "utf8"=>"✓"}
Show session dump

Show env dump

Response

Headers:

None

控制器

class Admin::ImagesController < ApplicationController
    respond_to :html, :json
    def index
        @album = Album.find(params[:album_id])
        @images = @album.images.all
    end
    # def new
    #   @album = Album.find(params[:album_id])
    #   @image = @album.images.new
    # end
    def new
        @album = Album.find(params[:album_id])
            @image = @album.images.new
    end
    def create
        @album = Album.find(params[:album_id])
        @image = @album.images.new(params[:image])

        if @image.save
            flash[:notice] = "Successfully added image!"
            redirect_to [:admin, @album, :images]
        else
            render :action => 'new'
        end
    end
    def show
        @album = Album.find(params[:album_id])
        @image = @album.images.find(params[:id])
    end
    def edit
        @album = Album.find(params[:album_id])
        @image = @album.images.find(params[:id])
    end
    def update
        @album = Album.find(params[:album_id])
        @image = @album.images.find(params[:id])
        if @image.update_attributes(params[:image])
            flash[:notice] = "Successfully updated Image"
            redirect_to [:admin, @album, :images]
        else
            render :action => "edit"
        end
    end
    def destroy
        @album = Album.find(params[:album_id])
        @image = @album.images.find(params[:id])
        @image.destroy
        @albumid = @album.id
        @id = @image.id
        FileUtils.remove_dir("#{Rails.root}/public/uploads/image/picture/#{@albumid}/#{@id}", :force => true)
        redirect_to admin_album_images_path(@album)
    end

end

模型

class Image < ActiveRecord::Base
    attr_accessible :title, :description, :picture, :image_id, :album_id, :albumcover
    belongs_to :album
    accepts_nested_attributes_for :album
    mount_uploader :picture, PictureUploader 
  end

1 个答案:

答案 0 :(得分:1)

这是我的设置,它通过将数组拆分为单独的字符串并将每个字符串发送到新对象来解决此错误。

<强>控制器

def create
        params[:image][:picture].each do |image|  //The :image is the object and the :picture is a row inside the :image where all files are being sent from the form.
            @album = Album.find(params[:album_id])
            @params = {}
            @params['picture'] = image
            @image = @album.images.create(@params)
       end
        if @image.save
            if params[:image][:picture].size > 1
                flash[:notice] = "Successfully added images!"
            else
                flash[:notice] = "Successfully added image!"
            end
            redirect_to [:admin, @album, :images]
        else
            render "new"
            flash[:notice] = "Did not successfully add image :("
        end
    end

查看表单

<%= form_for([:admin, :album, @image], :html => {:multipart => true}) do |f| %>
        <%= f.hidden_field :album_id, :value => @album.id %>
        <%= f.hidden_field :albumcover, :value => false %>
        <%= f.file_field :picture, :multiple => true %>
        <%= f.submit "Submit", :class => "submit" %>
<% end %>