获取提交按钮标签为“创建模型".而不是“更新模型"在rails的form_for内部 [英] Getting submit button label to be "Create model" instead of "Update model" inside form_for in rails

查看:67
本文介绍了获取提交按钮标签为“创建模型".而不是“更新模型"在rails的form_for内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用form_for和现有模型来呈现表单.我想提交该模型并将下一个要执行的动作设为创建"动作. 文档具有以下示例:

I am rendering a form using form_for with an existing model. I would like to submit that model and get the next action to be performed to be the 'create' action. The docs have this example:

<%= form_for @post do |f| %>
  <%= f.submit %>
<% end %>

并说在上面的示例中,如果@post是新记录,它将使用创建帖子"作为提交按钮标签,否则,它将使用更新帖子"."

And say "In the example above, if @post is a new record, it will use "Create Post" as submit button label, otherwise, it uses "Update Post"."

我对Rails还是比较陌生,不确定在文档中有关使用I18n进行自定义的内容.已有记录时,如何获取提交按钮以使用创建"?

I am relatively new to rails and am not sure what to make of the following stuff in the docs about customizing using I18n. How can I get the submit button to use "Create" when there's an existing record?

澄清. . .

我正在使用新动作来渲染form_for,但是我将其传递给了现有对象,以便可以对字段进行预填充.我希望它随后转到创建操作,但要转到更新.

I the form_for is being rendered out of the new action, but I am passing it an existing object, so that fields can be prepolulated. I want it to then go to the create action, but it is going to the update instead.

更新. .

我现在意识到问题出在form_for而不是提交,但是还没有弄清楚如何修改form_for以便将其发送到create动作.

I realize now that the issue is with the form_for and not the submit, but haven't yet figured out how to modify the form_for so that it sends to the create action.

推荐答案

http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-submit

共有三个选项,您可以在翻译文件(config/locales/en.yml)中覆盖它.

There are three options, you can either override it in the translations file (config/locales/en.yml).

en:
  helpers:
    submit:
      create: "Create %{model}"
      update: "Create %{model}"

或者您可以在Submit方法上指定一个值.

Or you can specify a value on the submit method.

f.submit("Create Post")

或者,您可以保留翻译文件的默认设置,然后执行以下操作:

Or, you can keep the translations file as it is by default and do:

f.submit(t('helpers.submit.create'))

这篇关于获取提交按钮标签为“创建模型".而不是“更新模型"在rails的form_for内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆