我在实现多个文件上传方面遇到了一些问题我已经尝试了几种方法让它工作,即答案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"}]>
然而,当我尝试显示它时,我得到“Locations#show中的NoMethodError”,“未定义的方法`url”代表#“
有人可以告诉我我做错了什么吗?我已经在这几天工作了,没有到任何地方。
我的其余代码是 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
位置控制器 class LocationsController&lt; ApplicationController的 .... #GET / locations #GET /locations.json def指数 @locations = Location.all @locations = @ locations.paginate(:page =&gt; 1,:per_page =&gt; 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处置:%20form数据;%20name = /%22location [图像] [] /%22;%20filename = /%22Cinque_Deck-and-Jacuzzi.jpg /%22 / r / nContent-Type:%20image / jpeg / r / n%22%7D'。在运行服务器的终端中
答案 0 :(得分:0)
好的,最后在装载了上传器的模型文件中有一个额外的s图像...令人尴尬但正在开发中正确解析数据