0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsの学習 ~画像アップロードにcarrierwaveを使う

Last updated at Posted at 2018-06-19

ユーザー登録時の画像や投稿機能の画像のアップロードにcarrierwaveを導入する手順のメモ

##gemを追加
まずはいつものようにgemを追加してインストールする

Gemfile
gem 'carrierwave'

##画像を保存するためのカラムを追加する
ここは適時、必要なテーブルに追加していく

$ rails g migrate add_column_to _users :image string

strong parameterで:imageを許可する

users_controller
def user_params
    params.requier(:user).permit(:name, :email, :password, :image)
end

##アップローダーの作成
$ rails g uploader image

##各モデルでアップローダーのマウント

user.rb
mount_uploader :image, ImageUploader

##フォームに下記を追加

edit.html.erb
<%=f.label :image,"ユーザー画像" %>
<%= f.file_field :image %>

##表示させる

view.html.erb
<%= image_tag @user.image %>
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?