[Enum_help] Use enum_help to create a select box displayed in Japanese!

Overview

Use the enum_help gem to record the process of creating the select box displayed in Japanese as a memorandum.

environment

・ Ruby '2.5.7' ・ Rails '5.2.3' ・ Enum_help '0.0.17'

process

1. Preparation

Add enum_help to your Gemfile.

gem 'enum_help'

"Bundle install" in the terminal.

bundle install

2. Add enum column to table

Add the target column.

db/migrate/20xxxxxxxxx_add_place_to_datespots.rb


class AddPlaceToDatespots < ActiveRecord::Migration[5.2]
  def change
    add_column :datespots, :place, :integer
  end
end

In the terminal, do "rails db: migrate".

rails db:migrate

3. Model definition

After preparing the enum column in the table, the next step is to associate multiple constants with the enum column.

app/models/datespot.rb


class Datespot < ApplicationRecord
(abridgement)
  enum place: {
    Ginza: 0, Yurakucho: 1, Shinbashi: 2, Hamamatsucho: 3, Tamachi: 4, Shinjuku: 5,
    Yoyogi: 6, Okubo: 7, Shibuya: 8, Omotesando: 9, Aoyama: 10, Harajuku: 11,
    Akasaka: 12, Roppongi: 13, Azabu: 14, Marunouchi: 15, Nihonbashi: 16, Kanda: 17,
    Ningyocho: 18, Ikebukuro: 19, Jiyugaoka: 20, Sangenjaya: 21, Futakotamagawa: 22, Shinagawa: 23,
    Gotanda: 24, Osaki: 25, Odaiba: 26, Toyosu: 27, ebisu: 28, Hiroo: 29,
    Shirogane: 30, Daikanyama: 31, Nakameguro: 32, Meguro: 33, Iidabashi: 34, Yotsuya: 35,
    Kagurazaka: 36, Ueno: 37, Asakusa: 38, Nakano: 39, Kichijoji: 40, Tachikawa: 41, Others: 42
  }
end

4. Definition of enum_help

Add a Japanese translation to config / locales / ja.yml.

config/locales/ja.yml


ja:
  enums:
    datespot:
      place:
        Ginza:Ginza
        Yurakucho:Yurakucho
        Shinbashi:Shimbashi
        Hamamatsucho:Hamamatsucho
        Tamachi:Tamachi
        Shinjuku:Shinjuku
        Yoyogi:Yoyogi
        Okubo:Okubo
        Shibuya:Shibuya
        Omotesando:Omotesando
        Aoyama:Aoyama
        Harajuku:Harajuku
        Akasaka:Akasaka
        Roppongi:Roppongi
        Azabu:Azabu
        Marunouchi:Marunouchi
        Nihonbashi:Nihonbashi
        Kanda:Kanda
        Ningyocho:Ningyocho
        Ikebukuro:Ikebukuro
        Jiyugaoka:Jiyugaoka
        Sangenjaya:Sangenjaya
        Futakotamagawa:Futakotamagawa
        Shinagawa:Shinagawa
        Gotanda:Gotanda
        Osaki:Osaki
        Odaiba:Odaiba
        Toyosu:Toyosu
        ebisu:Ebisu
        Hiroo:Hiroo
        Shirogane:platinum
        Daikanyama:Daikanyama
        Nakameguro:Nakameguro
        Meguro:Meguro
        Iidabashi:Iidabashi
        Yotsuya:Yotsuya
        Kagurazaka:Kagurazaka
        Ueno:Ueno
        Asakusa:Asakusa
        Nakano:Nakano
        Kichijoji:Kichijoji
        Tachikawa:Tachikawa
        Others:Other

5. Create select box

Create a select box using the convenience method of enum_help.

app/views/datespots/_datespot_form.html.erb


<%= form_with model: @datespot, local: true do |f| %>
(abridgement)
  <div class = 'form-group'>
    <%= f.label :place %> <span class="input-need">* Required</span>
    <%= f.select :place, options_for_select(Datespot.places_i18n.invert, selected: f.object.place), { include_blank: true }, { class: 'form-control', id: 'datespot_place' } %>
  </div>
<% end %>

I am making Japanese and English hashes with "Datespot.places_i18n.invert".

irb(main):001:0> Datespot.places_i18n.invert
=> {"Ginza"=>"Ginza", "Yurakucho"=>"Yurakucho", "Shimbashi"=>"Shinbashi", "Hamamatsucho"=>"Hamamatsucho", "Tamachi"=>"Tamachi", "Shinjuku"=>"Shinjuku", "Yoyogi"=>"Yoyogi
", "Okubo"=>"Okubo", "Shibuya"=>"Shibuya", "Omotesando"=>"Omotesando", "Aoyama"=>"Aoyama", "Harajuku"=>"Harajuku", "Akasaka"=>"Akasaka", "Roppongi"=>"Roppongi", "hemp
cloth"=>"Azabu", "Marunouchi"=>"Marunouchi", "Nihonbashi"=>"Nihonbashi", "Kanda"=>"Kanda", "Ningyocho"=>"Ningyocho", "Ikebukuro"=>"Ikebukuro", "Jiyugaoka"=>"Jiyugaoka",
 "Sangenjaya"=>"Sangenjaya", "Futakotamagawa"=>"Futakotamagawa", "Shinagawa"=>"Shinagawa", "Gotanda"=>"Gotanda", "Osaki"=>"Osaki", "Odaiba"=>"Odaiba", "Toyosu"=>"To
yosu", "Ebisu"=>"ebisu", "Hiroo"=>"Hiroo", "platinum"=>"Shirogane", "Daikanyama"=>"Daikanyama", "Nakameguro"=>"Nakameguro", "Meguro"=>"Meguro", "Iidabashi"=>"Iidaba
shi", "Yotsuya"=>"Yotsuya", "Kagurazaka"=>"Kagurazaka", "Ueno"=>"Ueno", "Asakusa"=>"Asakusa", "Nakano"=>"Nakano", "Kichijoji"=>"Kichijoji", "Tachikawa"=>"Tachikawa",
 "Other"=>"Others"}

Holds the value selected in "selected: f.object.place".

An empty element is added at the beginning with "include_blank: true".

Now you have the following select box! image.png

6. Receive the value in the view

By the way, when receiving a value in the view, if you add "_i18n", it will be displayed in Japanese.

app/views/datespots/_datespot.html.erb


(abridgement)
<%= datespot.place_i18n %>

reference

EnumHelp [Rails] enum tutorial

Recommended Posts

[Enum_help] Use enum_help to create a select box displayed in Japanese!
How to create a placeholder part to use in the IN clause
[Rails] How to use select boxes in Ransack
Try to create a bulletin board in Java
How to create a theme in Liferay 7 / DXP
How to easily create a pull-down in Rails
How to create a Java environment in just 3 seconds
How to create a Spring Boot project in IntelliJ
I tried to create a Clova skill in Java
How to create a data URI (base64) in Java
When you want to change the wording to be displayed when making a select box from enum
I want to create a form to select the [Rails] category
How to select a specified date by code in FSCalendar
I want to create a Parquet file even in Ruby
How to disable existing selected items in the select box
How to create a service builder portlet in Liferay 7 / DXP
How to create a method
Let's create a TODO application in Java 13 TODO form validation 1: Character limit ・ Gradle update to use @Validated
I tried to create a simple map app in Android Studio
How to create a new Gradle + Java + Jar project in Intellij 2016.03
Use Jenkins to build inside Docker and then create a Docker image.
How to use Lombok in Spring
Preparing to create a Rails application
[Rails] How to write in Japanese
Create a database in a production environment
Create a new app in Rails
[Java] How to create a folder
How to use InjectorHolder in OpenAM
Create a Servlet program in Eclipse
How to use classes in Java?
How to create a query using variables in GraphQL [Using Ruby on Rails]
I want to select multiple items with a custom layout in Dialog
To create a Zip file while grouping database search results in Java
Try to create a browser automatic operation environment using Selenide in 5 minutes
[Swift] If you want to use a URL that includes Japanese, use addingPercentEncoding.
Have a tool to create and open a new canvas in Mac preview
A memorandum on how to use Eclipse
Multilingual Locale in Java How to use Locale
How to insert a video in Rails
How to create a Maven repository for 2020
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
Create a TODO app in Java 7 Create Header
Create Docker to use Edge TPU compiler
Steps to set a favicon in Rails
[Swift5] How to create a splash screen
How to use environment variables in RubyOnRails
I want to use @Autowired in Servlet
How to publish a library in jCenter
Understand in 5 minutes !! How to use Docker
How to use credentials.yml.enc introduced in Rails 5.2
Create a prefectural select bar using active_hash
Steps to Use JConsole in Cognos Analytics
How to use ExpandableListView in Android Studio
How to display error messages in Japanese
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
Volume that wants to use a lot of logical operators in if statements
A memo to simply create a form using only HTML and CSS in Rails 6
[Introduction to Docker] Create a Docker image for machine learning and use Jupyter notebook
About the solution of the error that occurred when trying to create a Japanese file of devise in the Docker development environment