LoginSignup
0
0

Draperの使い方:モデルがファットにならないようデコレーターを活用する

Posted at

gem draperをインストールする

gemfileにgemを記載

gem 'draper'

bundle installする(下記はdocker使用の場合)

docker compose run web bundle install

システムにデコレーター層を追加

rails generate draper:install

デコレーターのファイルを追加

rails generate decorator ○○(モデル名)

モデル名_decorator.rbにコードを書く

lass UserDecorator < Draper::Decorator

 delegate_all

 def full_name
 "#{object.first_name} #{object.last_name}"
 end
end

viewファイルに記述を加える

<%= current_user.decorate.full_name %>

※サーバーの再起動を忘れずに・・

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