【问题标题】:Rails carrierwave multiple file upload errorRailscarrierwave多个文件上传错误
【发布时间】:2017-04-25 20:03:36
【问题描述】:

我在实现多个文件上传时遇到了一些问题,我尝试了几种方法来让它工作,即这里的答案Rails 4 multiple image or file upload using carrierwave。我无法获得第一个答案,但是我能够获得第二个答案,并且与carrierwave docs最相似的答案是向数据库中添加一个看起来像这样的条目; 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"}]>

但是当我尝试显示它时,我得到“NoMethodError in Locations#show”、“undefined method `url' for #”

有人可以告诉我我做错了什么吗?我已经为此工作了好几天了,却一无所获。

我的其余代码是 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

位置控制器 类位置控制器 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

谢谢,

编辑:还注意到这个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'.` 在运行服务器的终端中

【问题讨论】:

    标签: ruby-on-rails json image carrierwave


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2019-05-22
      相关资源
      最近更新 更多