Thursday, 4 October 2012

Paperclip Sample App

Paperclip Sample App

Step : 1
Lets First Set Your RVM

Step: 2
Let's get started by creating a new Rails application:

$ rails new paperclip-sample-app -d mysql
create create README.rdoc create Rakefile create create .gitignore create Gemfile create app create app/assets/images/rails.png create app/assets/javascripts/application.js create app/assets/stylesheets/application.css create app/controllers/application_controller.rb create app/helpers/application_helper.rb create app/mailers create app/models create app/views/layouts/application.html.erb create app/mailers/.gitkeep create app/models/.gitkeep create config create config/routes.rb create config/application.rb create config/environment.rb create config/environments create config/environments/development.rb create config/environments/production.rb create config/environments/test.rb create config/initializers create config/initializers/backtrace_silencers.rb create config/initializers/inflections.rb create config/initializers/mime_types.rb create config/initializers/secret_token.rb create config/initializers/session_store.rb create config/initializers/wrap_parameters.rb create config/locales create config/locales/en.yml create config/boot.rb create config/database.yml create db create db/seeds.rb create doc create doc/README_FOR_APP create lib create lib/tasks create lib/tasks/.gitkeep create lib/assets create lib/assets/.gitkeep create log create log/.gitkeep create public create public/404.html create public/422.html create public/500.html create public/favicon.ico create public/index.html create public/robots.txt create script create script/rails create test/fixtures create test/fixtures/.gitkeep create test/functional create test/functional/.gitkeep create test/integration create test/integration/.gitkeep create test/unit create test/unit/.gitkeep create test/performance/browsing_test.rb create test/test_helper.rb create tmp/cache create tmp/cache/assets create vendor/assets/javascripts create vendor/assets/javascripts/.gitkeep create vendor/assets/stylesheets create vendor/assets/stylesheets/.gitkeep create vendor/plugins create vendor/plugins/.gitkeep run bundle install

Step : 3

In your gem file add paperclip gem for image upload

gem "paperclip", :git => "git://"

Step : 4

After write add paperclip in your gem file

$ bundle install

Step : 5

I will just create a new "user" model with string columns for the name and email address:

$ rails g scaffold user name:string email:string

   invoke  active_record
      create    db/migrate/20121004063219_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/unit/user_test.rb
      create      test/fixtures/users.yml
      invoke  resource_route
       route    resources :users
      invoke  scaffold_controller
      create    app/controllers/users_controller.rb
      invoke    erb
      create      app/views/users
      create      app/views/users/index.html.erb
      create      app/views/users/edit.html.erb
      create      app/views/users/show.html.erb
      create      app/views/users/new.html.erb
      create      app/views/users/_form.html.erb
      invoke    test_unit
      create      test/functional/users_controller_test.rb
      invoke    helper
      create      app/helpers/users_helper.rb
      invoke      test_unit
      create        test/unit/helpers/users_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/
      invoke    scss
      create      app/assets/stylesheets/users.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss

Step : 6
Now we need to generate the database columns necessary for Paperclip on our new model object using rails

rails g paperclip user avatar
      create  db/migrate/20121004063230_add_attachment_avatar_to_users.rb

Step: 7
create table and run migration

$ rake db:create

$ rake db:migrate

==  CreateUsers: migrating ====================================================
-- create_table(:users)
   -> 0.7840s
==  CreateUsers: migrated (0.7841s) ===========================================

==  AddAttachmentAvatarToUsers: migrating =====================================
-- change_table(:users)
   -> 0.8870s
==  AddAttachmentAvatarToUsers: migrated (0.8872s) ============================

Step: 7

In your user model add attr_accessible of avatar

class User < ActiveRecord::Base
  attr_accessible :email, :name, :avatar

Step: 8
You can see that Paperclip generator created columns in the users table called "avatar_file_name", "avatar_content_type", "avatar_file_size", and "avatar_updated_at". Now we have our database schema setup.  The next step is to just modify the code that was generated for us by the scaffolding and make the changes necessary for paperclip.

The first thing to do is to add a line to the user model and indicate that it has a file attachment called "avatar". To do this, open app/models/user.rb and just add this one line:

class User < ActiveRecord::Base
  attr_accessible :email, :name, :avatar
  has_attached_file :avatar

And then edit the new user form (app/views/users/new.html.erb) and add a file field to use to upload files. There are actually two code changes you need to make: 

First you need to set the HTML form to encode the uploaded file data (and other fields) using MIME multiple part syntax, and then second you need to actually add the file upload field. Here's the finished new.html.erb file with these two changes in bold:

<%= form_for(@user, :html => {:multipart => true}) do |f| %>

and  second, add this p tag

<div class="field">
   <%= f.label :avatar %><br />
   <%= f.file_field :avatar %>

Step: 9

Now if we run our application we can upload an image file and attach it to a user:

if we submit the form, the image file will be uploaded to the server and saved on the file system. By default, paperclip saves files inside a "system" folder it creates in your Rails app's public folder. 

Step: 10
I'm almost done; now i just need to display the uploaded image somewhere; the simplest thing to do is just to add an image tag to the users show page.

  <%= image_tag @user.avatar.url %>

Step: 11
now we can see the image for our new user

No comments:

Post a Comment