[RUBY] How to use enum (introduction of Japanese notation)

This time, I used enum for the order status column when I was creating the EC site, and used the pull-down notation. Since this was my first time using an enum, I would like to leave an article as my own memo so that I will not forget it. The following articles were used as a reference when implementing.

(Reference article) -[For beginners] Use i18n to Japaneseize the f.select option of enum [Rails] https://qiita.com/tanutanu/items/d44a92425188a4489ec6 ・ [Beginner] Rails i18n supports Japanese localization https://qiita.com/shimadama/items/7e5c3d75c9a9f51abdd5

Gemfile

Gemfile.


gem 'enum_help'

terminal.


bundle install

First, write the gem required to use the enum in the Gemfile. After writing the above, execute the bundle install command in the terminal.

model.rb

order_item.rb


 enum production_status: {cannot_be_started: 0, waiting_for_production: 1, in_production: 2, production_completed: 3}

Next, add the description to the model file. enum column name: {name (this time status name): 0, name (this time status name): 1 ... n} After enum, the column name that uses enum and the name (English notation) and each value are numbered in order from 0 in the curly braces. In order to correspond numbers to each name in this way, the data type of enum when creating a table is integer (integer type).

Change to Japanese setting

config/application.rb


module NaganoApp
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    # config.load_defaults 5.2
    config.i18n.default_locale = :ja #Add only this one line
    
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

If this one line is not described, it will not be converted to Japanese notation.

ja.yml


ja:
  enums:
    order_item: 
      production_status:
        cannot_be_started: "Cannot be started"    
        waiting_for_production: "Waiting for production"
        in_production: "In production"
        production_completed: "Production completed"

The contents of the model file mentioned earlier are translated into Japanese and described. If there is an error in the description of the fault or column name, it will not be displayed.

Display in view

<%= form_with model: @order_item, url: admin_order_item_path(order_item), method: :patch, local: true do |f| %>
 <%= f.select :production_status, OrderItem.production_statuses.keys.map {|k| [I18n.t("enums.order_item.production_status.#{k}"), k]} %>
 <%= f.submit "Change" %>
<% end %>

The status can be changed by pull-down notation in the form. After f.select, it has the following structure, and if you make a mistake in the singular or plural notation of the column name, it will not be displayed. :Column name (singular),Model name.Column name (plural).keys.map {|k| [I18n.t("enums.Model name.Column name (singular).#{k}"), k]} % By the notation after keys.map, it becomes an image that the choices are entered in order from 0 of the set enum value.

Benefits of enum

This is a particularly effective function when frequent updates such as status management are required like this time. Since it is managed numerically, it is a big advantage that it can be easily corrected even when something needs to be changed. It is possible to make changes to the entire application by adding a few necessary descriptions to the model side and view.

the end

That's all for this time. I myself am a beginner in programming, but I hope it will be helpful to people in the same position. Also, if there are any mistakes in the content, we would appreciate it if you could point out.

Recommended Posts

How to use enum (introduction of Japanese notation)
[Rails] How to use enum
[Rails] How to use enum
How to use setDefaultCloseOperation () of JFrame
From introduction to use of ActiveHash
[Introduction to Rails] How to use render
How to use Java enum type
[Java] [Maven3] Summary of how to use Maven3
[Rails] Introduction of pry-rails ~ How to debug binding.pry
How to use Map
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
Output of how to use the slice method
How to use TreeSet
[How to use label]
How to use hashes
How to use JUnit 5
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Java] How to use compareTo method of Date class
[Ruby on Rails] How to write enum in Japanese
How to use CommandLineRunner in Spring Batch of Spring Boot
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
[Beginner] How to use devise Change settings from introduction
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"