【发布时间】:2015-06-21 19:07:43
【问题描述】:
我正在尝试在我的 rails 应用程序中使用carrierwave gem 实现上传图像,但是当我按下表单的提交按钮时,我收到以下错误:“无法将 nil 转换为字符串”.
应用程序跟踪:
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"。
【问题讨论】: