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.
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.
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).
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.
<%= 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.
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.
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