[Rails] How to install a decorator using gem draper

Purpose

Introduce a decorator using a gem draper. Make a note of the clogged part when introducing this Gem.

Development environment

・ Ruby 2.6.4 ・ Rails 5.2.3

Reference material

About the role of Decorator and Draper Summary of how to use Draper Prepare a Decorator to display Rails View to help Helper and Model

Installation procedure

① Install gem'draper'.

First, install Gem.

Gemfile.


gem 'draper'

You can use draper by typing the following two lines into the terminal.

$ bundle install
$ rails generate draper:install

② Create a decorator

First, create app/decorators/user_decorator.rb.

$ rails generate decorator User

This time we will create a method to call the full name. last_name and first_name have been added to the columns of the User model in advance.

app/decorators/user_decorator.rb


class UserDecorator < Draper::Decorator
  delegate_all

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

③ Call the created method in the View file.

Displays the currently logged in user (current_user) with the full name.

#In the View file where you want to use the method

<%= current_user.decorate.full_name %>

With the above procedure, you have installed the decorator using gem'draper'. If you get an error in this flow, please try to solve it while looking at the reference materials.

Recommended Posts

[Rails] How to install a decorator using gem draper
[Rails] How to create a graph using lazy_high_charts
[Rails] How to install devise
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use gem "devise"
[Rails] How to install Font Awesome
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
How to install Swiper in Rails
How to execute a contract using web3j
[Rails] How to upload images using Carrierwave
[Rails 6] How to create a dynamic form input screen using cocoon
How to insert a video in Rails
[rails] How to create a partial template
How to implement image posting using rails
[Rails] How to handle data using enum
How to implement a slideshow using slick in Rails (one by one & multiple by one)
How to create a query using variables in GraphQL [Using Ruby on Rails]
Rails: How to write a rake task nicely
[Rails] How to write when making a subquery
How to delete a controller etc. using a command
[Ethereum] How to execute a contract using web3j-Part 2-
[Rails] How to upload multiple images using Carrierwave
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to generate a primary key using @GeneratedValue
[Rails] How to create a Twitter share button
[Rails] Creating a breadcrumb trail using Gem gretel
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
How to write Rails
How to install Docker
How to uninstall Rails
How to install docker-machine
How to install ngrok
[Rails] How to create a signed URL for CloudFront
How to implement a like feature in Ajax in Rails
How to delete a new_record object built with Rails
How to manually generate a JWT with Rails Knock
How to delete custom Adapter elements using a custom model
How to install geckodriver (Selenium WebDriver) automatically using WebDriverManager
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
How to convert A to a and a to A using AND and OR in Java
[Rails 6] How to set a background image in Rails [CSS]
Rails learning How to implement search function using ActiveModel
[Rails] How to load JavaScript in a specific view
How to get started with creating a Rails app
How to implement a circular profile image in Rails using CarrierWave and R Magick
[rails] How to post images
[Rails] How to use enum
[Rails] How to use enum
How to leave a comment
How to read rails routes
How to install Boots Faces
How to use rails join
How to write Rails validation
[Rails] How to use validation
[Rails] How to use "kaminari"
[Rails] How to make seed