简体   繁体   English

载波文件上传

[英]Carrierwave File Upload

I am trying multiple file upload proposed by railcasts.com Jquery File Upload. 我正在尝试使用railcasts.com Jquery File Upload建议的多个文件上传。 I have followed the test, however it seem that everytime i refresh the server, nothing gets seen and in the following browser i get this 我已经进行了测试,但是似乎每次刷新服务器时都看不到任何东西,在以下浏览器中我得到了

  • google: blank icons google:空白图标
  • firefox: text "/assets/ Firefox:文字“ / assets /

Nothing seem to be uploaded in my folder. 我的文件夹中似乎什么也没有上传。 Where I would like my picture to be uploader would be public/upload/:imageid 我希望我的图片成为上传者的地方是public / upload /:imageid

Here my code Uploader 这是我的代码上传器

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

View 视图

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

Did i miss a step? 我错过了一步吗?

Model 模型

# == 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

View index.html.erb 查看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 %>

Controller 控制者

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 JavaScript ads.js.coffee

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

To me looks all good! 对我来说看起来不错!

Is your uploader mounted to your model? 您的上传器是否安装在模型上? If not, try this 如果没有,请尝试

mount_uploader :image, AimageUploader

and also, in your view, set the multiple option 并且在您看来,设置多选选项

<%= f.file_field :image, multiple: true, name: "model_name[image]" %>

I suggest you to use paperclip 我建议你使用回形针

Create a model called Image as follows: 创建一个名为Image的模型,如下所示:

class Image < ActiveRecord::Base
  has_attached_file :file
end

Let assume User model will have multiple images: 假设用户模型将具有多个图像:

class User < ActiveRecord::Base
  has_many :images
end

You can have separate image_controller to handle your file upload 您可以使用单独的image_controller来处理文件上传

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

相关问题 CarrierWave多个文件上传 - CarrierWave Multiple file Upload 使用Carrierwave和JQuery文件上载进行多文件上传 - Multiple File Upload using Carrierwave and JQuery File Upload 使用jQuery File Upload + Carrierwave + Rails 4启用异步文件上传 - Enabling Async file uploads with jQuery File Upload + Carrierwave + Rails 4 jQuery文件上传和Carrierwave检测文件夹和子文件夹 - JQuery File Upload and Carrierwave to Detect Folders and Sub Folders Rails 3 + Carrierwave + jQuery File Upload无法将图像文件上传到Openshift服务器 - Rails 3 + Carrierwave + jQuery File Upload fails to upload image file to Openshift server 多个文件选择使用carrierwave gem和jQuery-File-Upload库导致Rails服务器内部错误 - multiple file selection raise Server internal error in rails with carrierwave gem and jQuery-File-Upload library 在Rails中如何使用jquery-file-upload和CarrierWave处理多个多态文件上传? - In Rails how do I handle multiple, polymorphic file uploads with jquery-file-upload and CarrierWave? 使用Carrierwave上传之前预览图像 - Preview image before upload with Carrierwave 载波-从索引页面上传照片 - Carrierwave - upload photo from index page Ruby on Rails-Carrierwave,无法上传多个图像 - Ruby on Rails - Carrierwave, Can't upload multiple images
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM