Rails 6 - I18n key support for submit_tag key

Rails 6 added I18n key style support for submit tag.

Before Rails 6

Suppose we have a form for Admin::User model which expects first name and last name to create an admin user as shown below -

  <%= form_for @admin_user do |f| %>
    <div class="form-group">
      <%= f.label :first_name %>
      <%= f.text_field :first_name, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.label :last_name %>
      <%= f.text_field :last_name, class: "form-control" %>
    </div>
    
    <div class="actions">
      <%= f.submit class: "btn btn-lg btn-primary" %>
    </div>
  <% end %>

I18n translation key in en.yml is as below

  en:
    helpers:
      submit:
        admin_user:
          create: Create Admin User
    activerecord:
      models:
        admin/user: admin user
      attributes:
        admin/user:
          first_name: First name
          last_name: Last name      

Here we can see that the generated key admin_user nested under submit key, is not in I18n format but in <module_name>_<class_name> format.

After Rails 6

Due to the support of I18n format in Rails 6, key is now changed from <module_name>_<class_name> to module_name/class_name.

en.yml

  en:
    helpers:
      submit:
        admin/user:
          create: Create Admin User
    activerecord:
      models:
        admin/user: admin user
      attributes:
        admin/user:
          first_name: First name
          last_name: Last name      

This feature keeps all the I18n keys format consistent across all the components.

This change is backwards compatible, and so the old format can still be used in Rails 6.

Need help on your Ruby on Rails or React project?

Join Our Newsletter