0

我正在尝试 railcasts.com Jquery File Upload 提出的多个文件上传。我已经进行了测试,但是似乎每次我刷新服务器时,什么都看不到,在下面的浏览器中我得到了这个

  • 谷歌:空白图标
  • 火狐:文本“/资产/

我的文件夹中似乎没有上传任何内容。我希望我的图片成为上传者的地方是 public/upload/:imageid

这是我的代码上传器

class AimageUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper
  storage :file
  def store_dir
    "uploads/#{model.id}"
  end
  version :thumb do
    process :resize_to_fill => [200, 200]
  end
  def extension_white_list
    %w(jpg jpeg gif png)
  end   
end

看法

<div><%= image_tag(ad.aimage_url(:thumb)) %></div>

我错过了一步吗?

模型

# == Schema Information
#
# Table name: ads
#
#  id               :integer          not null, primary key
#  name             :string(255)
#  aimage           :string(255)
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#  advertisement_id :integer
#

class Ad < ActiveRecord::Base
  attr_accessible :aimage, :advertisement_id, :name
  mount_uploader :aimage, AimageUploader
end

查看 index.html.erb

<% @ads.each do |ad| %>
  <div><%= ad.name %></div>
  <%= image_tag(ad.aimage_url(:thumb)) if ad.aimage? %>
  <div>
    <%= link_to 'Show', ad %>
    <%= link_to 'Edit', edit_ad_path(ad) %>
    <%= link_to 'Destroy', ad, method: :delete, data: { confirm: 'Are you sure?' } %>
  </div>
<% end %>
<br />

<%= form_for Ad.new do |f| %>
  <div><%= f.label :aimage, "Upload advertisement:" %></div>
  <div><%= f.file_field :aimage, multiple: true, name: "advertisement[aimage]" %></div>
<% end %>

控制器

class AdsController < ApplicationController
  def index
    @ads = Ad.all
  end
  def show
    @ad = Ad.find(params[:id])
  end
  def new
    @ad = Ad.new
  end
  def edit
    @ad = Ad.find(params[:id])
  end
  def create
    @ad = Ad.create(params[:ad])
    if @ad.save
      flash[:notice] = "Successfully created advertisement."
      redirect_to root_url
    else
      render :action => 'new'
    end
  end
  def destroy
    @ad = Ad.find(params[:id])
    @ad.destroy
  end
end

Javascript ads.js.coffee

jQuery ->
  $('#new_ad').fileupload()

在我看来一切都很好!

4

2 回答 2

1

您的上传器是否安装到您的模型上?如果没有,试试这个

mount_uploader :image, AimageUploader

并且,在您看来,设置多个选项

<%= f.file_field :image, multiple: true, name: "model_name[image]" %>
于 2012-11-25T12:29:55.117 回答
0

我建议你用回形针

创建一个名为 Image 的模型,如下所示:

class Image < ActiveRecord::Base
  has_attached_file :file
end

假设用户模型将有多个图像:

class User < ActiveRecord::Base
  has_many :images
end

你可以有单独的 image_controller 来处理你的文件上传

于 2012-11-24T06:18:43.610 回答