Ruby on rails Rails Carrier有多个文件上载错误
我在实现多个文件上传时遇到了一些问题,我尝试了几种方法让它工作,即这里的答案。我无法得到第一个答案,但是我得到了第二个答案,也是与carrierwave文档最相似的一个,在数据库中添加了一个条目,如下所示:;Ruby on rails Rails Carrier有多个文件上载错误,ruby-on-rails,json,image,carrierwave,Ruby On Rails,Json,Image,Carrierwave,我在实现多个文件上传时遇到了一些问题,我尝试了几种方法让它工作,即这里的答案。我无法得到第一个答案,但是我得到了第二个答案,也是与carrierwave文档最相似的一个,在数据库中添加了一个条目,如下所示:; images:[{“tempfile”=>[],“original\u filename”=>“Cinque\u Deck-and-Jacuzzi.jpg”,“content\u type”=>“image/jpeg”,“headers”=>“content-Disposition:for
images:[{“tempfile”=>[],“original\u filename”=>“Cinque\u Deck-and-Jacuzzi.jpg”,“content\u type”=>“image/jpeg”,“headers”=>“content-Disposition:form data;name=\“location[images][],“location][images][],\r\n内容类型:image/jpeg\r\n},{“tempfile”=>,“original\u filename”=>,“cooking.jpeg”,“content\u-type”=>“image/jpeg”,“头文件”=>“内容配置:表单数据;名称=\“位置[图像][]”;文件名=\“烹饪.jpeg\”\r\n内容类型:图像/jpeg\r\n”}、{“临时文件”=>[]、“原始文件名”=>“悬挂摇滚.jpg”、“内容类型”=>“图像/jpeg”,“头文件”=>“内容配置:表单数据;名称=\“位置[图像][]”文件名=>“悬挂摇滚.jpg”\r\n内容类型:image/jpeg\r\n“}]>
然而,当我试图显示它时,我得到了“位置中的NoMethodError#show”,“未定义的#方法'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>
位置控制器
类位置控制器<应用程序控制器
....
#获取/定位
#GET/locations.json
def索引
@位置=位置.all
@位置=@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
谢谢
编辑:还注意到此
错误错误URI
/images/%7B%22tempfile%22=%3E[],%20%22原始文件名%22=%3E%22Cinque\u Deck-and-Jacuzzi.jpg%22,%20%22content\u类型%22=%3E%22image/jpeg%22,%20%22headers%22=%3E%22内容处理:%20表单数据;%20name=/%22location[图像][]/%22;%20filename=/%22Cinque_Deck-and-Jacuzzi.jpg/%22/r/n内容类型:%20image/jpeg/r/n%22%7D.“`在运行服务器的终端中好的,在装载上传器的模型文件中,图像上有一个额外的s。……令人尴尬,但正在正确解析数据好的,在模型中的图像上有一个额外的s装载上传程序的文件…但正在开发中正确解析数据
# 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