Ruby-on-rails Rails:如何更改 Rails 表单中提交按钮上的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4769483/
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-03 00:10:06  来源:igfitidea点击:

Rails: How to change the text on the submit button in a Rails Form

ruby-on-railsruby-on-rails-3

提问by Rod Nelson

i have listed my _form.html.erb file below what i would like to do is change the text on the submit button i know how to do it in html but not shure how to do it in Rails 3

我已经在下面列出了我的 _form.html.erb 文件我想做的是更改提交按钮上的文本我知道如何在 html 中执行但不知道如何在 Rails 3 中执行

%= form_for(@faq) do |f| %>
  <% if @faq.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@faq.errors.count, "error") %> prohibited this faq from being saved:</h2>

      <ul>
      <% @faq.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :question %><br />
    <%= f.text_field :question %>
  </div>
  <div class="field">
    <%= f.label :answer %><br />
    <%= f.text_area :answer %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

回答by Andrei S

instead of

代替

<%= f.submit  %>

put

<%= f.submit "My Submit Text" %>

回答by daniel

If you want to change all create and update form submit tags, this change is easy to make. Modify config/locales/en.ymllike so:

如果您想更改所有创建和更新表单提交标签,此更改很容易进行。config/locales/en.yml像这样修改:

en:
  helpers:
    submit:
      create: "Crear un %{model}"
      update: "Confirmar cambios al %{model} creado"

回答by Nathan Kot

Building on @daniel's answer, you can also customize submit tag values on a per-model basis:

基于@daniel 的回答,您还可以针对每个模型自定义提交标签值:

en:
  helpers:
    submit:
      model_name:
        create: "Create"
        update: "Update"

And then in your form you can just use:

然后在您的表单中,您可以使用:

<%= f.submit %>


See herefor the documentation (second example.)

有关文档,请参见此处(第二个示例。)

回答by tomeduarte

You can use:

您可以使用:

<%= f.submit 'Name of the submit button' %>

For questions like this, consider using the available docs either at

对于此类问题,请考虑使用以下可用文档

Sometimes, a google search like the one below helps:

有时,像下面这样的谷歌搜索会有所帮助:

回答by Pulkit Agarwal

When writing in erb

erb写的时候

<%= f.submit "your text" %>

<%= f.submit "your text" %>

when writing in HAML

HAML编写时

= f.button :submit, "your text"

In HAMLcomma should be there after submit otherwise it will throw error.

HAML 中,提交后应该有逗号,否则会抛出错误。

回答by Claudio Shigueo Watanabe

I had this problem and I only had to translate the model name this way:

我遇到了这个问题,我只需要这样翻译模型名称:

pt-br:
  activerecord:
    models:
      user:
        one: "Usuário"
        more: "Usuários"

This also would complement @daniel's answer which gave me the hint what was missing. However, I suppose that @daniel's answer is not really necessary as it is already on rails-i18n

这也将补充@daniel 的回答,这给了我缺少的提示。但是,我认为@daniel 的回答并不是真正必要的,因为它已经在rails-i18n 上

回答by arogachev

Sometimes using helpers is not acceptable because of used text or you need additionally add class etc., so you can directly override value:

有时使用助手是不可接受的,因为使用了文本或者您需要额外添加类等,因此您可以直接覆盖value

<%= f.submit class: 'btn btn-primary', value: 'Login' %>

or:

或者:

<%= f.button :submit, class: 'btn btn-primary', value: 'Login' %>

By the way it was mentioned by @cassi.lupin comment to accepted answer.

顺便说一下,@cassi.lup在对已接受答案的评论中提到了这一点。

Tested on Rails 4.2.3.

在 Rails 4.2.3 上测试。

回答by Asif Ahmed

for Slim version use value="xyz" to change the default submit input text.

对于 Slim 版本,使用 value="xyz" 更改默认提交输入文本。

回答by Cris R

Just in case, I was trying with this scenario:

以防万一,我正在尝试这种情况:

f.submit t('conf.begin') class: 'btn btn-outline btn-success'

But it was not working, the solution was with a comma before the class (it was not obvious at the begining for me):

但它不起作用,解决方案是在课前加一个逗号(一开始对我来说并不明显):

f.submit t('conf.begin'), class: 'btn btn-outline btn-success'

Cheers

干杯

回答by Ajey

Its simple, use

它的简单,使用

<%= f.submit 'Desired text on the button' %>