CarrierWaveで画像をアップロードする
carrierwaveuploader/carrierwave · GitHubを使うと簡単に画像をアップロードできます。
1. インストール
Gemfileに以下追記して、bundle install.
gem 'carrierwave' gem 'rmagick'
エラーになる場合
bundle installで以下のエラーになりました。
Gem::Ext::BuildError: ERROR: Failed to build gem native extension. /home/ubuntu/.rbenv/versions/2.2.1/bin/ruby -r ./siteconf20150521-25259-1kwt7md.rb extconf.rb checking for gcc... yes checking for Magick-config... yes checking for outdated ImageMagick version (<= 6.4.9)... *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. Provided configuration options: --with-opt-dir --without-opt-dir --with-opt-include --without-opt-include=${opt-dir}/include --with-opt-lib --without-opt-lib=${opt-dir}/lib --with-make-prog --without-make-prog --srcdir=. --curdir --ruby=/home/ubuntu/.rbenv/versions/2.2.1/bin/$(RUBY_BASE_NAME) extconf.rb:79:in ``': No such file or directory - convert (Errno::ENOENT) from extconf.rb:79:in `block in configure_compile_options' from /home/ubuntu/.rbenv/versions/2.2.1/lib/ruby/2.2.0/mkmf.rb:911:in `block in checking_for' from /home/ubuntu/.rbenv/versions/2.2.1/lib/ruby/2.2.0/mkmf.rb:351:in `block (2 levels) in postpone' from /home/ubuntu/.rbenv/versions/2.2.1/lib/ruby/2.2.0/mkmf.rb:321:in `open' from /home/ubuntu/.rbenv/versions/2.2.1/lib/ruby/2.2.0/mkmf.rb:351:in `block in postpone' from /home/ubuntu/.rbenv/versions/2.2.1/lib/ruby/2.2.0/mkmf.rb:321:in `open' from /home/ubuntu/.rbenv/versions/2.2.1/lib/ruby/2.2.0/mkmf.rb:347:in `postpone' from /home/ubuntu/.rbenv/versions/2.2.1/lib/ruby/2.2.0/mkmf.rb:910:in `checking_for' from extconf.rb:76:in `configure_compile_options' from extconf.rb:15:in `initialize' from extconf.rb:474:in `new' from extconf.rb:474:in `<main>' extconf failed, exit code 1 Gem files will remain installed in /var/share/karte/vendor/bundle/ruby/2.2.0/gems/rmagick-2.15.0 for inspection. Results logged to /var/share/karte/vendor/bundle/ruby/2.2.0/extensions/x86-linux/2.2.0-static/rmagick-2.15.0/gem_make.out An error occurred while installing rmagick (2.15.0), and Bundler cannot continue. Make sure that `gem install rmagick -v '2.15.0'` succeeds before bundling.
これはImageMagickがインストールされていないからみたいなので、
インストールしてみます。
$ sudo aptitude install imagemagick
これで再度bundle installすると、今度はインストールできました。
2. Uploaderを生成する
$ bundle exec rails g uploader image create app/uploaders/image_uploader.rb
3. 画像をアップロードするモデルにカラムを追加する
Itemモデルにimageカラムを追加します。
$ bundle exec rails g migration add_image_to_items image:string invoke active_record create db/migrate/20150521044125_add_image_to_items.rb $ bundle exec rake db:migrate RAILS_ENV=xxx
4. Uploaderをマウントする
モデルItemに、以下を追記します。
mount_uploader :image, ImageUploader
5. Uploaderを編集する
6. Viewを編集する
- ファイルをアップロードするところ
_form.html.erb
<%= f.file_field :image %>
- アップロードしたファイルを表示するところ
show.html.erb
<%= image_tag(@item.image.thumb.url, size: "100x100", class:"img-thumbnail") %> <%= @item.image.url %> #=> ファイルのパス(APP_ROOT/publicからのパス) <%= @item.image_identifier %> #=> ファイル名 <%= @item.image.thumb.url %> #=> サムネイルファイルのパス