LoginSignup
26
47

More than 5 years have passed since last update.

refile使い方

Last updated at Posted at 2018-04-05

インストール

リファイルgem

  • ファイルのアップロード先を柔軟に設定できる
  • 簡単に組み込める
  • 画像のサムネイルを生成できる

imagemagickインストール
$ sudo apt-get update
$ sudo apt-get -y install imagemagick libmagick++-dev

gemに記述

Gemfile
gem "refile", require: "refile/rails", github: 'manfe/refile'
gem "refile-mini_magick"

その後に$ bundle install

実装

モデルにカラムを持たせる(image_id)

$ rails g migration add_image_id_to_posts image_id:string
$ rails db:migrate

attachment(アタッチメント)メソッドを持たせる

image_idを持たせたモデルにattachement :image

model/image_idを持ったモデル
class PostImage < ApplicationRecord

  attachment :image # ここを追加(_idは含めない)

end

ファイルアップロードのフォーム

<%= f.attachment_field :image %>
カラム名はimage_idでも:imageを引数にする

画面に埋め込む

attachment_image_tag モデル名, :image, :fill, 横幅の数値(例: 100), 縦幅の数値, format: '拡張子名(例: jpg)' 
26
47
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
26
47