1
votes

I've been experiencing some issues with implementing multiple file uploads i've tried a few ways to get it to work namely the answers here Rails 4 multiple image or file upload using carrierwave. I couldn't get the first answer to work however i was able to get the second answer, and the one most similar to the carrierwave docs to add a entry to the database which looked like this; images: [{"tempfile"=>[], "original_filename"=>"Cinque_Deck-and-Jacuzzi.jpg", "content_type"=>"image/jpeg", "headers"=>"Content-Disposition: form-data; name=\"location[images][]\"; filename=\"Cinque_Deck-and-Jacuzzi.jpg\"\r\nContent-Type: image/jpeg\r\n"}, {"tempfile"=>[], "original_filename"=>"cooking.jpeg", "content_type"=>"image/jpeg", "headers"=>"Content-Disposition: form-data; name=\"location[images][]\"; filename=\"cooking.jpeg\"\r\nContent-Type: image/jpeg\r\n"}, {"tempfile"=>[], "original_filename"=>"hanging-rock.jpg", "content_type"=>"image/jpeg", "headers"=>"Content-Disposition: form-data; name=\"location[images][]\"; filename=\"hanging-rock.jpg\"\r\nContent-Type: image/jpeg\r\n"}]>

However when i try to display it, i get "NoMethodError in Locations#show", "undefined method `url' for #"

Can someone please tell me what i'm doing wrong? I've been working on this for days now and not getting anywhere.

The rest of my code is show.html.erb

    <%= image_tag @location.images[0].url, class: "display-location animated bounce" %>
<div class = "row hidden-sm-down">
   <div class = "col-sm-4 hidden-sm-down">
      <a href = "#" class = "thumbnail">
         <%= image_tag @location.images[1].url %>
      </a>
   </div>
   <div class = "col-sm-4">
      <a href = "#" class = "thumbnail">
         <%= image_tag @location.images[2].url %>
      </a>
   </div>
   <div class = "col-sm-4">
      <a href = "#" class = "thumbnail">
         <%= image_tag @location.images[3].url %>
      </a>
   </div>
</div>

schema.rb

    ActiveRecord::Schema.define(version: 20161210123055) do
  enable_extension "plpgsql"

  create_table "locations", force: :cascade do |t|
    t.string   "name"
    t.string   "address"
    t.string   "website"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.string   "image"
    t.text     "description"
    t.string   "price"
    t.json     "images"
  end

locations controller class LocationsController < ApplicationController .... # GET /locations # GET /locations.json def index @locations = Location.all @locations = @locations.paginate(:page => 1, :per_page => 2) end

  # GET /locations/1
  # GET /locations/1.json
  def show
    @random_location = Location.where.not(id: @location).order("RANDOM()").first(3)
    @reviews = Review.where(location_id: @location.id).order("created_at DESC")
    if @reviews.blank?
      @avg_rating = 0
    else
      @avg_rating = @reviews.average(:rating).round(2)
    end
  end

  # GET /locations/new
  def new
    @location = Location.new
  end

  # GET /locations/1/edit
  def edit
  end

  # POST /locations
  # POST /locations.json
  def create
    @location = Location.new(location_params)

    respond_to do |format|
      if @location.save
        format.html { redirect_to @location, notice: 'Location was successfully created.' }
        format.json { render :show, status: :created, location: @location }
      else
        format.html { render :new }
        format.json { render json: @location.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /locations/1
  # PATCH/PUT /locations/1.json
  def update
    respond_to do |format|
      if @location.update(location_params)
        format.html { redirect_to @location, notice: 'Location was successfully updated.' }
        format.json { render :show, status: :ok, location: @location }
      else
        format.html { render :edit }
        format.json { render json: @location.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /locations/1
  # DELETE /locations/1.json
  def destroy
    @location.destroy
    respond_to do |format|
      format.html { redirect_to locations_url, notice: 'Location was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_location
      @location = Location.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def location_params
      params.require(:location).permit(:name, :price, :address, :website, :description, {images: []})
    end

    def check_user
      unless current_user.admin?
        redirect_to root_url, alert: "Sorry currently only admins have that functionality"
      end
    end
end

Thanks,

EDIT: Also noticed this ERROR bad URI/images/%7B%22tempfile%22=%3E[],%20%22original_filename%22=%3E%22Cinque_Deck-and-Jacuzzi.jpg%22,%20%22content_type%22=%3E%22image/jpeg%22,%20%22headers%22=%3E%22Content-Disposition:%20form-data;%20name=/%22location[images][]/%22;%20filename=/%22Cinque_Deck-and-Jacuzzi.jpg/%22/r/nContent-Type:%20image/jpeg/r/n%22%7D'.` in the terminal running the server

1

1 Answers

0
votes

okay ended up having a extra s on images in the model file where the uploader is mounted... embarrassing but parsing data correctly now in development