Skip to content

Instantly share code, notes, and snippets.

@ginpei
Last active August 7, 2018 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ginpei/6270837 to your computer and use it in GitHub Desktop.
Save ginpei/6270837 to your computer and use it in GitHub Desktop.
Ruby on Rails+CarrierWave+RMagickで画像アップローダーをささっと作る手順。https://ginpen.com/2013/08/20/carrierwave/
echo == アプリ作成 ==
rails new uploader
cd uploader
echo == CarrierWave導入 ==
echo "gem 'carrierwave'" >> Gemfile
bundle
echo == 投稿モデル作成 ==
rails g scaffold post name:string comment:text
echo == アップロード機能を追加 ==
rails g uploader Image
rails g migration add_images_to_posts image:string
sed -i -e "1a\\ \\ mount_uploader :image, ImageUploader" app/models/post.rb
sed -i -e "21a\\ \\ <div class="field">\\n\
\\ \\ \\ \\ <%= f.label :image %><br>\\n\
\\ \\ \\ \\ <%= f.file_field :image %>\\n\
\\ \\ </div>" app/views/posts/_form.html.erb
sed -i -e "72 s/permit(\\(.*\\))/permit(\\1, :image)/" app/controllers/posts_controller.rb
sed -i -e "11a<p><%= image_tag @post.image_url %></p>" app/views/posts/show.html.erb
echo == まとめてDB作成 ==
rake db:migrate
echo == RMagick導入 ==
echo "gem 'rmagick'" >> Gemfile
bundle
echo == リサイズ表示 ==
sed -i -e "6 s/# //" app/uploaders/image_uploader.rb
sed -i -e "35,37 s/# //" app/uploaders/image_uploader.rb
sed -i -e "36 s/:scale/:resize_to_fit/" app/uploaders/image_uploader.rb
sed -i -e "7a\\ \\ \\ \\ \\ \\ <th>Thumbnail<\/th>" app/views/posts/index.html.erb
sed -i -e "19a\\ \\ \\ \\ \\ \\ \\ \\ <td><%= image_tag post.image_url(:thumb) %><\/td>" app/views/posts/index.html.erb
echo おしまい。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment