How to upload picture in rails app ?

If you want to upload picture in your rails app, you should make

  • Open your Gemfile in rails app then add
1
gem 'paperclip', :git => "git://github.com/thoughtbot/paperclip.git"

in terminal write bundle for install gem.

  • Which models do you want to upload picture ? For example, you have books model, because of this, I write all command according to book model.

  • After install paperclip gem, open your terminal again then write

1
 $ rails g paperclip Book photo

then

1
$ bundle exec rake db:migrate
  • Open models/book.rb add
1
2
3
4
5
6
7
  include Paperclip::Glue
  has_attached_file :photo, :styles => { :small => "250x250>" },
                    :url  => "/assets/books/:id/:style/:basename.:extension",
                    :path => ":rails_root/public/assets/books/:id/:style/:basename.:extension"
  validates_attachment_presence :photo
  validates_attachment_size :photo, :less_than => 10.megabytes
  validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
  • Open books/show.html.haml add
1
= image_tag @book.photo.url(:small)
  • Open books/_form.html.haml add
1
2
= form_for @book, html: { multipart: true }  do |f|
  = f.file_field :photo
  • Open controllers/books_controller.rb, then check create method if you use
1
2
3
 def create
    @book = Book.new(book_params)
 end

instead of above, you should write

1
2
3
 def create
    @book = Book.create(book_params)
 end

Lastly, you should control to add :photo in your book_params method.

1
2
3
4
private
    def book_params
      params.require(:book).permit(:category_id, :author_id, :isbn, :name, :photo)
    end

If you want to learn with details, you can visit this site

That’ s all.

Comments