简体   繁体   English

Carrierwave没有上传文件(Rails 4)

[英]Carrierwave not uploading file (Rails 4)

I'm having trouble uploading files through Carrierwave. 我在通过Carrierwave上传文件时遇到问题。 My models are set up so that there are many bottles, and each bottle can have multiple swatches. 我的模型设置为有很多瓶子,每个瓶子可以有多个样本。 Each swatch is an image uploaded through Carrierwave. 每个样本都是通过Carrierwave上传的图像。 However, the files aren't saving at all. 但是,文件根本不保存。 No files are showing up in the directory I want it to save to, and calling if @bottle.swatches.any? 我希望它保存到的目录中没有文件出现, if @bottle.swatches.any?调用if @bottle.swatches.any? in the views doesn't show anything. 在视图中没有显示任何内容。

This is my first time using Rails 4, so I have a feeling my mistake might be due to nooby practices (and not actually Carrierwave). 这是我第一次使用Rails 4,所以我觉得我的错误可能是由于nooby实践(而不是实际的Carrierwave)。 I've looked all over stack, but nothing I've tried is working. 我看起来遍布堆栈,但我尝试过的都没有工作。 What am I doing wrong? 我究竟做错了什么? Thanks so much in advance! 非常感谢提前!

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

Controllers 控制器

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 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) 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'

In console logs after hitting 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"}

I think the problem is with your strong params. 我认为问题在于你强大的障碍。 Try this: 试试这个:

params.require(:bottle).permit({whatever bottle inputs you have},
 swatches_attributes: [{whatever swatch inputs you have, including the picture}])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM