Rails 3 和 carrierwave:无法将 nil 转换为 String

Rails 3 and carrierwave: can't convert nil into String

我试图在我的 rails 应用程序中使用载波 gem 实现上传图像,但是当我按下表单的提交按钮时,我收到以下错误: "can't convert nil into String".

应用程序跟踪:

app/controllers/uploads_controller.rb:48:in `new'
app/controllers/uploads_controller.rb:48:in `create'

要求:

{"utf8"=>"✓",
"authenticity_token"=>"VsgoOE/BQwwT0aQRRMO22kxdPgC/BS2H/CnHVBi4Cfw=",
"upload"=>{"name"=>"aaa",
"description"=>"aa",
"image"=>[#<ActionDispatch::Http::UploadedFile:0x508a8b0 @original_filename="c1147374806dce0ec967015c041fec3b.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"upload[image][]\"; filename=\"c1147374806dce0ec967015c041fec3b.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:C:/Users/user/AppData/Local/Temp/RackMultipart20150621-5356-1gm0atx>>]},
"commit"=>"Upload"}

上传模型:

class Upload < ActiveRecord::Base
    validates_presence_of :user_id, :image
    attr_accessible :name, :description, :image
    mount_uploader :image, ImageUploader
end

上传控制器:

class UploadsController < ApplicationController
    before_filter :signed_in_user

    def index
    (...)
    end

    def show
    (...)
    end

    def new
        @upload = Upload.new
        respond_to do |format|
            format.html # new.html.erb
            format.json { render json: @upload }
        end
    end

    def edit
        (...)
    end

    def create
        @upload =  Upload.new(params[:upload])
        @upload.user_id = current_user.id
        respond_to do |format|
            if @upload.save

                format.html { redirect_to @upload, notice: 'Image was uploaded.' }
                format.json { render json: @upload, status: :created, location: @upload }

            else
                format.html { render action: "new" }
                format.json { render json: @upload.errors, status: :unprocessable_entity }
            end
        end
    end

    def update
        (...)
    end

    def destroy
        (...)
    end
end

上传表格 (uploads/_form.html.erb):

<%= form_for @upload, :html => {:id => "upload_form", :multipart => true} do |f| %>
    <div class="field">
        <%= f.text_field :name, placeholder: 'Title' %>
    </div>
    <div class="field">
        <%= f.text_field :description, placeholder: 'Description' %>
    </div>
    <%= f.file_field :image, multiple: true %>
    <div class="actions">
        <%= f.submit "Upload" %>
    </div>
<% end %>

载波上传者:

class ImageUploader < CarrierWave::Uploader::Base
    storage :file

    def store_dir
        "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
end

上传架构:

create_table "uploads", :force => true do |t|
    t.string   "name"
    t.string   "description"
    t.integer  "user_id"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
    t.string   "image"
end

carrierwave图片保存目录:

public
    uploads
        upload
            image
                1
                ...

应用程序跟踪引用的第 48 行是 @upload = Upload.new(params[:upload])

我该如何解决这个问题? 我不明白为什么上传是 nil。 谁能告诉我正确的方法?

一些细节:OS Win7 Pro,Rails v.3.2.21,Carrierwave v.0.10.0,如 IDE Rubymine v.7.1.2

编辑 1: 根据@sreena 的建议,我从上传切换到图像,现在这是请求消息:

Parameters: {"utf8"=>"✓",
"authenticity_token"=>"G1zpdyCbXRJYqAR5m5hscMjQOry85KN7toNhJznOFok=",
"upload"=>{"name"=>"titolo", "description"=>"descrizione",
"image"=>[#<ActionDispatch::Http::UploadedFile:0x534aef0 @original_filename="2.jpg", 
@content_type="image/jpeg", @headers="Content-Disposition: form-data;
name=\"upload[image][]\"; filename=\"2.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:C:/Users/user/AppData/Local/Temp/RackMultipart20150624-4180-14oypoq>>]},
"commit"=>"Upload"}

按下提交按钮后,现在我收到另一个从表单中引发的错误:"Image can't be blank"

你应该像这样在控制器中传递创建参数

@upload = Upload.new(参数[:图片])

,应该是图片没有上传

在您的 UploadsContoller 页面中添加此功能 def upload_params params.require(:upload).permit(:image,:name,:description) end

然后在 def 中像这样创建添加

def create @upload = Upload.new(upload_params) @upload.user_id = current_user.id respond_to do |format| if @upload.save

            format.html { redirect_to @upload, notice: 'Image was uploaded.' }
            format.json { render json: @upload, status: :created, location: @upload }

        else
            format.html { render action: "new" }
            format.json { render json: @upload.errors, status: :unprocessable_entity }
        end
    end
end

我终于解决了这个问题。 我只是通过以下方式更改创建方法

def create
    @upload =  current_user.uploads.build(params[:upload])
    @upload.image = params[:upload]["image"]
    respond_to do |format|
    if @upload.save

        format.html { redirect_to @upload, notice: 'Image was uploaded.' }
        format.json { render json: @upload, status: :created, location: @upload }

    else
        format.html { render action: "new" }
        format.json { render json: @upload.errors, status: :unprocessable_entity }
    end
end

这让我觉得你的 routes.rb 文件中有一些无效的东西。

检查路由语法和所有版本...

并检查 gem carrierwave 版本...