Ruby-on-rails rails form_for 标签的自定义文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13003626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:54:31  来源:igfitidea点击:

Custom text for rails form_for label

ruby-on-railsrubyruby-on-rails-3.2form-for

提问by Paul S.

I want to display a label in form_for:

我想在以下位置显示标签form_for

<div class="field">
  <%= f.label :name %><br />
  <%= f.text_field :name %>
</div>

This generates the label "Name", but I want it to be "Your Name". How can I change it?

这会生成标签“姓名”,但我希望它是“你的姓名”。我怎样才能改变它?

回答by gylaz

The second parameter to labelhelper will allow you to set custom text.

labelhelper的第二个参数将允许您设置自定义文本。

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

Use Ruby on Rails Documentationto look up helper methods.

使用Ruby on Rails 文档查找辅助方法。

回答by hjing

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:

您可以通过 i18n 指定自定义标签文本。在 中config/locales/en.yml,假设您的用户模型名为user,您可以添加以下内容:

helpers:
    label:
      user:
        name: Your Name

This will allow you to keep using

这将允许您继续使用

<%= f.label :name %>

without having to hardcode Your Name.

无需硬编码Your Name

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

有关 i18n 的更多信息,请参阅。有关的文档label请参阅

回答by RodneyH

i18n with rails 5.2.2 this works perfectly.

i18n 与 rails 5.2.2 这完美地工作。

Translate labels, placeholders, and buttonson deviseforms 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

本地文件: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

回答by NemyaNation

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

在 Rails 5.1.0 上,上面接受的答案不起作用。

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

传递的第一个参数可以用作自定义标签。

<%= f.label :mobile, "Mobile No:" %>