У меня возникли проблемы с загрузкой файлов через Carrierwave. Мои модели настроены так, что есть много бутылок, и каждая бутылка может иметь несколько образцов. Каждый образец представляет собой изображение, загруженное через Carrierwave. Однако файлы не сохраняются вообще. Никакие файлы не отображаются в каталоге, который я хочу сохранить, и при вызове if @bottle.swatches.any?
в представлениях ничего не отображается.Carrierwave not uploading file (Rails 4)
Это мой первый раз, когда я использую Rails 4, поэтому я чувствую, что моя ошибка может быть связана с практикой nooby (а не на самом деле Carrierwave). Я просмотрел весь стек, но ничего, что я пробовал, работает. Что я делаю не так? Большое спасибо заранее!
class Swatch < ActiveRecord::Base
belongs_to :user
belongs_to :bottle, dependent: :destroy
mount_uploader :swatch, SwatchUploader
validates :user_id, presence: true
validates :bottle_id, presence: true
end
class Bottle < ActiveRecord::Base
belongs_to :user
has_many :swatches
has_many :favorite_bottles
has_many :favorited_by, through: :favorite_bottles, source: :user
accepts_nested_attributes_for :swatches
validates :name,
presence: true,
length: { in: 1..100 }
end
Контроллеры
class BottlesController < ApplicationController
def new
@bottle = Bottle.new
@bottle.swatches.build
end
def create
@bottle = Bottle.new(bottle_params)
if @bottle.save
redirect_to @bottle
else
render 'new'
end
end
#... edit, show, index... etc
private
def bottle_params
params.require(:bottle).permit(:name, :brand, :color, :finish, :swatch, :swatches)
end
swatch_uploader.rb
class SwatchUploader < CarrierWave::Uploader::Base
storage :file
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"#{Rails.root}/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
end
HTML (HAML)
= form_for @bottle, html: { multipart: true } do |f|
- if @bottle.errors.any?
.alert.alert-danger
- @bottle.errors.full_messages.each do |msg|
= msg
.col-md-6
%p
= f.fields_for :swatches do |swatch|
= swatch.label :swatch
= swatch.file_field :swatch
%p
= f.label :name
= f.text_field :name
%p
= f.submit 'Submit'
В журналах консоли после удара представить:
Started POST "/bottles" for 127.0.0.1 at 2013-11-08 10:25:54 -0500
Processing by BottlesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eOIHJNs6quv2OQQpp2tDn+9+d8rY8oiTjUI/gyp1bn4=", "bottle"=>{"swatches_attributes"=>{"0"=>{"swatch"=>#<ActionDispatch::Http::UploadedFile:0x007fb6aac322c0 @tempfile=#<Tempfile:/var/folders/pw/hw3vcmbs3s39dp2l89j0y23r0000gn/T/RackMultipart20131108-97524-dkcjzh>, @original_filename="Malice.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"bottle[swatches_attributes][0][swatch]\"; filename=\"Malice.jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "name"=>"sdfsdf", "commit"=>"Submit"}
Вы можете показать свою бутылку_парамов – Philip7899
@ Philip7899 добавлен! – Michelle