0

How can I upload multiple images using CarrierWave?

I followed "drjorgepolanco" answer from

Rails 4 multiple image or file upload using carrierwave

but I keep getting a error

    undefined method `[]' for #<AssignmentpicUploader:0x007eff7831a4d8>
    Extracted source (around line #25):

    ActionView::Template::Error (undefined method `[]' for #<AssignmentpicUploader:0x007eff7831a4d8>):
    22:    
    23:    
    24:    <div id="img">
    25:     <%= image_tag @assignment.picture[1].url(:large) %>
    26:   </div>
    27:   
    28:   </br>
  app/views/assignments/show.html.erb:25

assignment.rb

class Assignment < ActiveRecord::Base
        validates :name, length: { minimum: 1, maximum:120 }
        validates :description, length: { minimum: 1 }
        mount_uploader :picture, AssignmentpicUploader
end

assignments_controller.rb

class AssignmentsController < ApplicationController
  before_action :authenticate_user!
  before_filter :admin_access, only: [:new, :create, :edit, :update, :destroy]


    def index
     @assignments = Assignment.all.order("created_at DESC")
    end


   def show
     @assignment = Assignment.find(params[:id])
   end

   def new
     @assignment = Assignment.new
   end  

   def create
     @assignment = current_user.assignments.build
     @assignment.name = params[:assignment][:name]
     @assignment.description = params[:assignment][:description]
     @assignment.picture = params[:assignment][:picture]


     if @assignment.save
       flash[:notice] = "Assignment was saved successfully."
       redirect_to @assignment
     else
       flash.now[:alert] = "Error creating assignment. Please make sure there is a name and description."
       render :new
     end
   end

     def edit
     @assignment = Assignment.find(params[:id])
     end

      def update
     @assignment = Assignment.find(params[:id])

     @assignment.name = params[:assignment][:name]
     @assignment.description = params[:assignment][:description]
     @assignment.picture = params[:assignment][:picture]

     if @assignment.save
        flash[:notice] = "Assignment was updated successfully."
       redirect_to @assignment
     else
       flash.now[:alert] = "Error saving assignment. Please try again."
       render :edit
     end
      end

   def destroy
     @assignment = Assignment.find(params[:id])

     if @assignment.destroy
       flash[:notice] = "\"#{@assignment.name}\" was deleted successfully."
       redirect_to action: :index
     else
       flash.now[:alert] = "There was an error deleting the assignment."
       render :show
     end
   end

     private 

     def assignment_params
         params.require(:assignment).permit(:name, :description, picture: [])
     end
end

assignments/new.html.erb

        ...
       <div class="col-md-8">
         <%= form_for @assignment, :html => { :multipart => true } do |f| %>
           <div class="form-group">
             <%= f.label :name %>
             <%= f.text_field :name, class: 'form-control', placeholder: "Enter assignment name" %>
           </div>
           <div class="form-group">
             <%= f.label :description %>
             <%= f.text_area :description, rows: 8, class: 'form-control', placeholder: "Enter assignment description" %>
           </div>
           <div class="form-group">
          <%= f.label :picture %>
          <%= f.file_field :picture, :multiple => true %>
          </div>
          <br />
           <%= f.submit "Save", class: 'button' %>
         <% end %>
       </div>
     </div>

assignments/show.html.erb

    <%= image_tag @assignment.picture[1].url(:large) %>

schema.rb

create_table "assignments", force: :cascade do |t|
    t.string   "name"
    t.text     "description"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "user_id"
    t.string   "picture"
  end

  add_index "assignments", ["user_id"], name: "index_assignments_on_user_id"
Community
  • 1
  • 1
Mike Stamb
  • 17
  • 8
  • Set a breakpoint before ```flash.now[:alert]... ``` in create action and check the result of ```@assignment.errors```. Someone can help you only with detailed info about error – Nadiya Oct 20 '16 at 08:45
  • how does one set a breakpoint? sorry im still a noob. Do I need a debugger gem to do this? – Mike Stamb Oct 20 '16 at 08:52
  • You do. I would recommend to use https://github.com/pry/pry – Nadiya Oct 20 '16 at 08:56

1 Answers1

0

Carrierwave documentation says:

Add a column which can store an array. This could be an array column or a JSON column for example. Your choice depends on what your database supports.

Your "picture" is just a string. Add serialize :picture, JSON to your model.

Nadiya
  • 1,421
  • 4
  • 19
  • 34
  • Thanks that stopped the validation error message and pointed me to the code causing the error, which I have now put in my question. – Mike Stamb Oct 20 '16 at 13:45
  • As you can see in the error, the problem is with array that can't be stored as a string. Please do the suggestions from my answer. – Nadiya Oct 20 '16 at 13:53
  • I have added "serialize :picture, JSON" to my assignment.rb file but how can i change :picture which is a string to a :JSON column type? – Mike Stamb Oct 20 '16 at 20:55
  • There is no such kind of column type "json". You convert an array to json object and save it in a "string" column. – Nadiya Oct 21 '16 at 12:48