LoginSignup
1
1

More than 3 years have passed since last update.

(Rails)画像投稿機能[refile refile-mini_magick]

Posted at

前提

  • ImageMagickインストールしておいてください
Gemfile
gem "refile", require: "refile/rails", github: 'manfe/refile'
gem "refile-mini_magick"

Gemfileに記述後
$ bundle installします。

<h1>画像投稿フォーム</h1>
<%= form_with model: @post, local:true do |f| %>
  <h4>画像</h4>
  <%= f.attachment_field :image %>
  <%= f.submit '投稿' %>
<% end %>

f.attachment_field使うことで投稿できるようになります。

modelを追加

post.rb
  attachment :image 

_idを抜いた部分を記述します。

画像表示

posts/show.html.erb
<%= attachment_image_tag @post, :image, format: 'jpeg', fallback: "no-image-icon.jpg", size: "120x120" %>

画像がなかったらfallback: "no-image-icon.jpg"を表示する。
imageフォルダーに適当な画像を入れる。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1