У меня возникли проблемы с реализацией нескольких загрузок файлов. Я пробовал несколько способов заставить его работать, а именно ответы здесь Rails 4 multiple image or file upload using carrierwave. Я не мог получить первый ответ на работу, но мне удалось получить второй ответ, а тот, который больше всего похож на документы с несущей, добавить запись в базу данных, которая выглядела так: 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"}]>
Ошибка загрузки нескольких файлов несущей с Rails
Однако, когда я пытаюсь отобразить его, я получаю «NoMethodError в Locations # шоу», «неопределенный метод` URL»для #»
Может кто-то пожалуйста, скажите мне, что я делаю неправильно? Я работаю над этим уже несколько дней и никуда не денусь.
Остальная часть моего кода show.html.erb
schema.rb
ActiveRecord::Schema.define(version: 2016121) 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
контроллер местоположения класса LocationsController < ApplicationController .... # GET/местоположения # GET/locations.json def index @locations = Lo cation.all @locations = @ locations.paginate (: страница => 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
Спасибо,
EDIT: Кроме того, заметил это ERROR bad URI
/изображения /% 7B % 22tempfile% 22% 3E = [],% 20% 22% 22original_filename =% 3E%% 22% 20% 22% 22content_type =% 3E% 22image/JPEG%, 22% 20% 22headers 22Cinque_Deck-и-Jacuzzi.jpg % 22% = 3E% 22Content-Disposition:% 20form-данных;% = 20name /% 22location [изображения] [] /% 22;% = 20filename /% 22Cinque_Deck-и-Jacuzzi.jpg /% 22/г/nContent- Тип:% 20image/jpeg/r/n% 22% 7D'.` в терминале, на котором запущен сервер