简体   繁体   中英

Carrierwave File Upload

I am trying multiple file upload proposed by 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
  • firefox: text "/assets/

Nothing seem to be uploaded in my folder. Where I would like my picture to be uploader would be 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

<% @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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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