SlideShare a Scribd company logo
File uploading through Paperclip in rails 3.x




Paperclip is an easy file attachment library for Rails applications. Attached files are saved to the
file system, database or cloud and referenced in the browser by an easily understandable
specification.
Here is an example to explain the image attachment for a user profile in an application. This
example narrates about saving the image in the file system. However, the images can be saved in
S3 bucket [Amazon Simple Storage Service, S3] or database.
Step#1

ImageMagick must be installed in the system and Paperclip must have access to it. ImageMagick
is a software suite to create, edit, compose, or convert bitmap images. It can read and write
images in a variety of formats. You can download it by visiting the following URL:



 http://www.imagemagick.org/script/index.php


Step#2

Include the paperclip gem in your Gemfile



 gem "paperclip"


Then run the bundler to install the gem



 bundle install


Step#3

Add the fields for the image processing as below in your migration file
class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
        t.string :name
        t.string :photo_file_name
        t.string :photo_content_type
        t.string :photo_file_size
        t.timestamps
    end
  end
end


Then run your migration files



  rake db:migrate


Step#4

Modify the user model for cropping and save image in your system folder



has_attached_file :photo, :styles => { :small => "150x150>" },
  :url => "/system/:attachment/:id/:style/:basename.:extension",
  :path =>
":rails_root/public/system/:attachment/:id/:style/:basename.:extension"


Here you can specify the file path and the image size as per the requirement.


Step#5

Modify your view file to upload an image



   <%= form_for :user,:url => {:action => "create"}, :html => { :multipart
=> true } do |f| %>
       <%= f.file_field :photo %>
     <% end %>


Step#6

Write the following code to display the image of a user

    <%= image_tag @user.photo.url (:small)%>

More Related Content

DOC
Rails notification
PDF
Web Programming - 10 JavaScript
PDF
Web Programming - 6 Bootstrap Framework
PPTX
Database and Web Integration
PPTX
Code Coverage Web Application
PPT
Asp.net,mvc
PPTX
Asp.net With mvc handson
PPTX
BITSAA.org Backend Administration - Using Resource Manager for Custom Pages
Rails notification
Web Programming - 10 JavaScript
Web Programming - 6 Bootstrap Framework
Database and Web Integration
Code Coverage Web Application
Asp.net,mvc
Asp.net With mvc handson
BITSAA.org Backend Administration - Using Resource Manager for Custom Pages

Similar to File uploading through paperclip in rails 3.x (20)

PPTX
Getting started with rails active storage wae
PDF
File Upload 2015
PDF
QConSP 2015 - Dicas de Performance para Aplicações Web
PDF
Rails Plugins - Linux For You, March 2011 Issue
PDF
Paperclip
PPT
Useful Rails Plugins
PPT
Rails e suas Gems
PDF
If you doing file uploads with rails you're gonna have a bad time
PDF
Rails course day 7
PDF
 Active Storage - Modern File Storage? 
PDF
OSDC 2009 Rails Turtorial
PDF
Rails2 Pr
KEY
More to RoC weibo
PDF
Paperclip
PDF
Rails3 changesets
PDF
Rails 3 Beautiful Code
ZIP
Rails 3 (beta) Roundup
PDF
The Rails Way
PDF
Extracting Plugins And Gems From Rails Apps
PDF
Ruby On Rails Starter Kit
Getting started with rails active storage wae
File Upload 2015
QConSP 2015 - Dicas de Performance para Aplicações Web
Rails Plugins - Linux For You, March 2011 Issue
Paperclip
Useful Rails Plugins
Rails e suas Gems
If you doing file uploads with rails you're gonna have a bad time
Rails course day 7
 Active Storage - Modern File Storage? 
OSDC 2009 Rails Turtorial
Rails2 Pr
More to RoC weibo
Paperclip
Rails3 changesets
Rails 3 Beautiful Code
Rails 3 (beta) Roundup
The Rails Way
Extracting Plugins And Gems From Rails Apps
Ruby On Rails Starter Kit
Ad

More from Andolasoft Inc (20)

PDF
Scalable Mobile App Development for Business Growth1 (1).pdf
PDF
Latest Facts and Trends in Fitness App Development
PDF
Challenges of React Native App Development_ Effective Mitigation Strategies.pdf
PDF
How To Use Server-Side Rendering with Nuxt.js
PDF
Essential Functionalities Your Real Estate Web App Must Have.pdf
PDF
A Complete Guide to Developing Healthcare App
PDF
Game-Changing Power of React Native for Businesses in 2024
PDF
A Complete Guide to Real Estate Website Development
PDF
How to Build Cross-Platform Mobile Apps Using Python
PDF
Impact of AI on Modern Mobile App Development
PDF
How to Optimize the SEO of Shopify Stores
PDF
14 Tips On How To Improve Android App Performance
PDF
The Ultimate Guide to Setting Up Your WooCommerce Store
PDF
Ranking The Best PHP Development Companies in the World
PDF
Top 8 WordPress Design and Development Trends of 2023
PDF
List of 10 Best WordPress Development Companies
PDF
WooCommerce vs Shopify: Which is Better For Your Online Store
PDF
Why Choose WooCommerce For Your eCommerce Store
PDF
Django Workflow and Architecture
PDF
Service Oriented Architecture in NodeJS
Scalable Mobile App Development for Business Growth1 (1).pdf
Latest Facts and Trends in Fitness App Development
Challenges of React Native App Development_ Effective Mitigation Strategies.pdf
How To Use Server-Side Rendering with Nuxt.js
Essential Functionalities Your Real Estate Web App Must Have.pdf
A Complete Guide to Developing Healthcare App
Game-Changing Power of React Native for Businesses in 2024
A Complete Guide to Real Estate Website Development
How to Build Cross-Platform Mobile Apps Using Python
Impact of AI on Modern Mobile App Development
How to Optimize the SEO of Shopify Stores
14 Tips On How To Improve Android App Performance
The Ultimate Guide to Setting Up Your WooCommerce Store
Ranking The Best PHP Development Companies in the World
Top 8 WordPress Design and Development Trends of 2023
List of 10 Best WordPress Development Companies
WooCommerce vs Shopify: Which is Better For Your Online Store
Why Choose WooCommerce For Your eCommerce Store
Django Workflow and Architecture
Service Oriented Architecture in NodeJS
Ad

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Spectroscopy.pptx food analysis technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Electronic commerce courselecture one. Pdf
PPTX
MYSQL Presentation for SQL database connectivity
Big Data Technologies - Introduction.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Unlocking AI with Model Context Protocol (MCP)
Programs and apps: productivity, graphics, security and other tools
Spectroscopy.pptx food analysis technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Empathic Computing: Creating Shared Understanding
Encapsulation_ Review paper, used for researhc scholars
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Reach Out and Touch Someone: Haptics and Empathic Computing
Network Security Unit 5.pdf for BCA BBA.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
cuic standard and advanced reporting.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Electronic commerce courselecture one. Pdf
MYSQL Presentation for SQL database connectivity

File uploading through paperclip in rails 3.x

  • 1. File uploading through Paperclip in rails 3.x Paperclip is an easy file attachment library for Rails applications. Attached files are saved to the file system, database or cloud and referenced in the browser by an easily understandable specification. Here is an example to explain the image attachment for a user profile in an application. This example narrates about saving the image in the file system. However, the images can be saved in S3 bucket [Amazon Simple Storage Service, S3] or database. Step#1 ImageMagick must be installed in the system and Paperclip must have access to it. ImageMagick is a software suite to create, edit, compose, or convert bitmap images. It can read and write images in a variety of formats. You can download it by visiting the following URL: http://www.imagemagick.org/script/index.php Step#2 Include the paperclip gem in your Gemfile gem "paperclip" Then run the bundler to install the gem bundle install Step#3 Add the fields for the image processing as below in your migration file
  • 2. class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :name t.string :photo_file_name t.string :photo_content_type t.string :photo_file_size t.timestamps end end end Then run your migration files rake db:migrate Step#4 Modify the user model for cropping and save image in your system folder has_attached_file :photo, :styles => { :small => "150x150>" }, :url => "/system/:attachment/:id/:style/:basename.:extension", :path => ":rails_root/public/system/:attachment/:id/:style/:basename.:extension" Here you can specify the file path and the image size as per the requirement. Step#5 Modify your view file to upload an image <%= form_for :user,:url => {:action => "create"}, :html => { :multipart => true } do |f| %> <%= f.file_field :photo %> <% end %> Step#6 Write the following code to display the image of a user <%= image_tag @user.photo.url (:small)%>