I'm trying to implement upload image with carrierwave gem in my rails application, but when I press the submit button of the form I recive the following error: "can't convert nil into String".
Application trace:
app/controllers/uploads_controller.rb:48:in `new'
app/controllers/uploads_controller.rb:48:in `create'
Request:
{"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"}
upload model:
class Upload < ActiveRecord::Base
validates_presence_of :user_id, :image
attr_accessible :name, :description, :image
mount_uploader :image, ImageUploader
end
upload controller:
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
upload form (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 %>
carrierwave uploader:
class ImageUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
upload schema:
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
Directory where carrierwave save images:
public
uploads
upload
image
1
...
The line 48 refers by application trace is @upload = Upload.new(params[:upload])
.
How can I fix this issue?
I can't figure why upload is nil
.
Can someone put me in the right way?
Some details: OS Win7 Pro, Rails v.3.2.21, Carrierwave v.0.10.0, as IDE Rubymine v.7.1.2
EDIT 1: As suggest from @sreena I switched from upload to image and now this is the request message:
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"}
After press the submit button, now I recive another error raises from the form: "Image can't be blank"
.
Finally I solved the issue. I just change create method in the following way