Custom text for rails form_for label

∥☆過路亽.° 提交于 2019-11-30 00:12:02

The second parameter to label helper will allow you to set custom text.

<%= f.label :name, 'Your Name' %>

Use Ruby on Rails Documentation to look up helper methods.

You can specify custom label text via i18n. In config/locales/en.yml, and assuming your user model is named user, you can add the following:

helpers:
    label:
      user:
        name: Your Name

This will allow you to keep using

<%= f.label :name %>

without having to hardcode Your Name.

For more information about i18n, see this. Documentation on the label refer to this.

i18n with rails 5.2.2 this works perfectly.

Translate labels, placeholders, and buttons on devise forms or other forms.

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
   <div class="mt-3">
     <label class="float-left"> <%= f.label t(:email) %> </label>
       <%= f.email_field :email, class: 'form-control', placeholder: t('.emailholder') %>
   </div>
   <div class="mt-3">
     <label class="float-left"> <%= f.label t(:password) %> </label>
       <%= f.password_field :password, class: 'form-control', placeholder: t('.passholder') %>
   </div>

   <div class="button">
     <%= f.button t('.signinbtn'), class: "" %>
   </div>
<% end %>

locals file: config/locales/en.yml

en:
  activerecord:
    ....others

  #Found in Views/devise/seasions/new <form> <*label*>
  email: "Email"
  password: "Password"

  #Views/devise <form> <placeholder & buttom>
  devise: #if your using devise forms
    #seasions/new.html.erb
    new:
      emailholder: "enter email here"
      passholder: "enter password"
      signinbtn: "SignIn"

  ....others
NemyaNation

On Rails 5.1.0 , the accepted answer above does not work.

The first parameter passed can be used as a customized label.

<%= f.label :mobile, "Mobile No:" %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!