Ruby on rails 轨道3和托架波形:can';t将nil转换为字符串
我试图在rails应用程序中使用carrierwave gem实现上传图像,但当我按下表单的提交按钮时,我收到了以下错误:“无法将nil转换为字符串” 应用程序跟踪:Ruby on rails 轨道3和托架波形:can';t将nil转换为字符串,ruby-on-rails,carrierwave,Ruby On Rails,Carrierwave,我试图在rails应用程序中使用carrierwave gem实现上传图像,但当我按下表单的提交按钮时,我收到了以下错误:“无法将nil转换为字符串” 应用程序跟踪: app/controllers/uploads_controller.rb:48:in `new' app/controllers/uploads_controller.rb:48:in `create' 请求: {"utf8"=>"✓", "authenticity_token"=>"VsgoOE/BQwwT0aQ
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"}
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"}
参数:{“utf8”=>“✓",
“真实性令牌”=>“G1zpdyCbXRJYqAR5m5hscMjQOry85KN7toNhJznOFok=“,
“上传”=>{“名称”=>“标题”、“说明”=>“说明”,
“图像”=>[#]},
“提交”=>“上载”}
按下submit按钮后,现在我收到表单中出现的另一个错误:
“图像不能为空”
您应该像这样在控制器中传递创建参数
@upload=upload.new(参数[:图像])
,它应该是图像不上传在上传者页面中添加此功能
def上传参数
参数require(:upload).permit(:image,:name,:description)
结束
在def中创建如下添加
def创建
@上传=上传。新建(上传参数)
@upload.user\u id=当前用户id
回应待办事项|格式|
如果@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
最后我解决了这个问题。 我只是用以下方式更改create方法
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
我听从了你的建议,但现在我收到了一条新的错误消息:“图像不能为空”(当我按下提交按钮时)。它似乎无法识别图像。编辑中的新请求错误。很抱歉延迟,当我尝试上载图像时,添加了该功能,但我收到消息“无法将nil转换为字符串”“,所以我认为这应该是一个gem版本的问题。无论如何,谢谢你的建议!
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
public
uploads
upload
image
1
...
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"}
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