首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >载波不上传文件(Rails 4)

载波不上传文件(Rails 4)
EN

Stack Overflow用户
提问于 2013-11-08 15:12:58
回答 1查看 4.2K关注 0票数 6

我很难通过载波上传文件。我的模型是建立的,以便有很多瓶,每个瓶子可以有多个样本。每个样本都是通过载波上传的图像。然而,这些文件根本没有保存。在我希望它保存到的目录中没有文件显示,在视图中调用if @bottle.swatches.any?没有显示任何内容。

这是我第一次使用Rails 4,所以我觉得我的错误可能是由于nooby实践(而不是实际的载波)。我找遍了所有的东西,但我试过的东西都没有用。我做错了什么?提前谢谢你!

代码语言:javascript
复制
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

控制器

代码语言:javascript
复制
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

代码语言:javascript
复制
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)

代码语言:javascript
复制
= 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'

在控制台日志中,点击submit后:

代码语言:javascript
复制
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"}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-08 15:58:46

我认为问题在于你的强力对立面。试试这个:

代码语言:javascript
复制
params.require(:bottle).permit({whatever bottle inputs you have},
 swatches_attributes: [{whatever swatch inputs you have, including the picture}])
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19862324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档