i'm making a form to upload multiple images with carrierwave. Save array to db work fine but i want to save 1 array element as 1 record, to make it easier to manage later. I tried to do as below but it return nilClass err.
All picture use the same description, name is image file name
Anyone has done this kind of stuff before :'(
views/_form
<%= form_for [:admin, @picture], :html => {multipart: true, :class => 'form-horizontal'} do |f| %>
<div class="box-body">
<div class="form-group">
<%= f.label :link, :class => 'col-sm-2 control-label' %>
<div class="col-sm-10">
<%= f.file_field :link, multiple: true, :class => 'form-control', :id => 'imgInp' %>
</div>
</div>
<div class="form-group">
<%= f.label :description, :class => 'control-label col-sm-2' %>
<div class="col-sm-10">
<%= f.text_area :description, :class => 'form-control' %>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<%= link_to admin_pictures_path do %>
<button class="btn btn-default">Back</button>
<% end %>
<%= f.submit nil, :class => 'btn btn-info pull-right' %>
</div>
<!-- /.box-footer --> <% end %>
controller
def create
if params[:link]
params[:link].each { |image|
@picture = Picture.new(name: image.file.filename, description: params[:description], link: image)
@picture.save
}
end
end
respond_to do |format|
if @picture.save
format.html { redirect_to admin_pictures_path, notice: ' picture was successfully created.' }
format.json { render :show, status: :created, location: @picture }
else
format.html { render :new }
format.json { render json: @picture.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_picture
@picture = Picture.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def picture_params
params.require(:picture).permit(:name, :description, :link)
end
end
param[:link] console
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"2KMy3lPMnf3MqLLHpJAH+Ei40nja+KAJGsx2twyP5L05A95b7rLsZHEFoUSu+CMJQunQ4yUq6kmppdK6I7NQkw==",
"picture"=>{"album_id"=>"3", "team_id"=>"", "link"=>[#<ActionDispatch::Http::UploadedFile:0x007f9095ea1c08 @tempfile=#<Tempfile:/tmp/RackMultipart20160331-6776-1h8i98y.jpg>,
@original_filename="images.jpg", @content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"picture[link][]\";
filename=\"images.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f9095ea1be0 @tempfile=#<Tempfile:/tmp/RackMultipart20160331-6776-y6d6ug.jpg>, @original_filename="images (1).jpg", @content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"picture[link][]\";
filename=\"images (1).jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f9095ea1bb8 @tempfile=#<Tempfile:/tmp/RackMultipart20160331-6776-hizrku.jpg>,
@original_filename="lulu.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"picture[link][]\";
filename=\"lulu.jpg\"\r\nContent-Type: image/jpeg\r\n">],
"description"=>"abc"}, "commit"=>"Create Picture"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
params:
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.4ms)
params.inspecthash from console ?createmethod add puts "params: #{params[:link]}" on first line & submit your form then in console copy theputsresult and paste in the question.params[:picture][:link]notparams[:link]