简体   繁体   English

Rails的载波多文件上传错误

[英]Rails carrierwave multiple file upload error

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 . 我在实现多个文件上载时遇到了一些问题,我尝试了几种方法使其工作,即在这里找到答案Rails 4使用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 #" 但是,当我尝试显示它时,出现“ Locations#show中的NoMethodError”,“#的未定义方法'url'”

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 我其余的代码是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 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 位置控制器类LocationsController <ApplicationController ....#GET / locations#GET /locations.json def索引@locations = Location.all @locations = @ locations.paginate(:page => 1,:per_page => 2)结束

  # 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 编辑:还注意到此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-和-Jacuzzi.jpg /%22 / r / nContent-Type:%20image / jpeg / r / n%22%7D'。在运行服务器的终端中

好的,最终在安装了上传器的模型文件中的图像上有一个额外的s ...令人尴尬,但现在正在开发中的数据解析正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM