Coder Social home page Coder Social logo

jhjguxin / rails-uploader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from superp/rails-uploader

1.0 1.0 0.0 144 KB

Rails file upload implementation with jQuery-File-Upload

Home Page: https://github.com/blueimp/jQuery-File-Upload

License: MIT License

rails-uploader's Introduction

HTML5 File uploader for rails

This gem use https://github.com/blueimp/jQuery-File-Upload for upload files.

Preview:

Uploader in use

Install

In Gemfile:

gem "rails-uploader"

In routes:

mount Uploader::Engine => '/uploader'

Usage

Architecture to store uploaded files (cancan integration):

class Asset < ActiveRecord::Base
  include Uploader::Asset
  
  def uploader_create(params, request = nil)
    ability = Ability.new(request.env['warden'].user)
    
    if ability.can? :create, self
      self.user = request.env['warden'].user
      super
    else
      errors.add(:id, :access_denied)
    end
  end
  
  def uploader_destroy(params, request = nil)
    ability = Ability.new(request.env['warden'].user)
    
    if ability.can? :delete, self
      super
    else
      errors.add(:id, :access_denied)
    end
  end
end

class Picture < Asset
  mount_uploader :data, PictureUploader
  
  validates_integrity_of :data
  validates_filesize_of :data, :maximum => 2.megabytes.to_i
end

For example user has one picture:

class User < ActiveRecord::Base
  has_one :picture, :as => :assetable, :dependent => :destroy
  
  fileuploads :picture
end

Find asset by foreign key or guid:

@user.fileupload_asset(:picture)

Include assets

Javascripts:

//= require uploader/application

Stylesheets:

*= require uploader/application  

Views

<%= uploader_field_tag :article, :photo %>

or FormBuilder:

<%= form.uploader_field :photo %>

Formtastic

<%= f.input :picture, :as => :uploader %>

SimpleForm

<%= f.input :picture, :as => :uploader %>

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Copyright (c) 2012 Aimbulance, released under the MIT license

rails-uploader's People

Contributors

galetahub avatar superp avatar alainpilon avatar

Stargazers

jhjguxin avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.