0

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)
8
  • Can you post the params.inspect hash from console ? Commented Mar 31, 2016 at 4:19
  • @MuhammadYawarAli: er? how should i type that? I'm a newbie Commented Mar 31, 2016 at 4:21
  • In your create method add puts "params: #{params[:link]}" on first line & submit your form then in console copy the puts result and paste in the question. Commented Mar 31, 2016 at 4:24
  • @MuhammadYawarAli updated Q Commented Mar 31, 2016 at 4:36
  • 1
    @Okami Actually, its params[:picture][:link] not params[:link] Commented Mar 31, 2016 at 6:38

2 Answers 2

1

This will solve your issue :

def create
    if params[:picture][:link]
    params[:picture][:link].each { |image|
      @picture = Picture.new(:name => image.original_filename,:description => params[:description])
      @picture.store!(image.tempfile)
      @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
Sign up to request clarification or add additional context in comments.

9 Comments

it gives me 'undefined method `save' for nil:NilClass' at respond format @picture.save
Answer updated try now. or add this @picture.store!(File.open(image.tempfile.path))
same error, seem like it not go in if statement. add a puts but console not return
yes you can get filename by : image.original_filename and which gem you are using for image saving ?
in my case i have this mount_uploader :file, UserUploader in my User model so i am saving file as : @user.file.store!(image.tempfile) edit store statement as per your model details.
|
0

Try

...
@picture = Picture.new(picture_params)
respond_to do |format|
    if @picture.save...

so that @picture.save will not result to nil

3 Comments

the default is working fine, but it gonna store a lot of link as array in 1 record. I dont want that
Just like what I've said create a detail table for your picture
i created it at first. it have 6 column: id, name, desc, link, created_at, updated_at

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.