Git Product home page Git Product logo

paperclip-practice's Introduction

Paperclip Practice - Soccer Teams

This repo was created as a practice to learn the Paperclip gem, from Thoughbot.

What is Paperclip?

Paperclip is a gem that integrates the ability to upload files into our database. It was developed by Thoughtbot and available as an open source repo on GitHub.

Setting it Up

Follow the steps on the Paperclip's README for complete instructions, but here is a lightweight overview of the most basic parts:

Install ImageMagick

ImageMagick is a prerequisite for Paperclip, so make sure to install it first by running:

brew install imagemagick

There are additional requirements for Windows machines. Be sure to check out Paperclip's README for specific Windows instructions

Include Paperclip in the Gemfile

Add Paperclip to the Gemfile through the following code:

gem "paperclip", "~> 4.2"

Integrating Paperclip Into Your App

Migrations

The rails generate paperclip <model_name> <file_field_name> will generate the following migration to add the file field:

class AddFieldColumnsToModel < ActiveRecord::Migration
  def self.up
    add_attachment :<model_name>, :<file_field_name>
  end

  def self.down
    remove_attachment :<model_name>, :<file_field_name>
  end
end

Views

New

Within the 'new' view form, add the tag <%= form.file_field :<field_name> %> in order to allow for the upload file button to appear.

Also, be sure to include the html: { :multipart => true } to the form_for tag as an optional argument. This ensures that you can add the file appropriately since Paperclip actually generates more than one field to cover for all of the aspects of the file.

Show

Add it as an image tag in Ruby: <%= image_tag @user.avatar.url %>

Controllers

Add the most basic version needed, without specific styles as:

class <model_name> < ActiveRecord::Base
  has_attached_file :<field_name>
  validates_attachment_content_type :<field_name>, :content_type => /\Aimage\/.*\Z/
end

paperclip-practice's People

Contributors

rfleury2 avatar

Watchers

James Cloos avatar  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.