Getting submit button label to be “Create model” instead of “Update model” inside form_for in rails

瘦欲@ 提交于 2019-12-06 12:04:15

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

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}"

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'))

form_for will update a existing record instead of create, so basically you have to feed it a new record. The best way to do this is in the controller with record.dup. Something like

@post = @existing_post.dup

dup will create a shallow copy, allowing you to save it as a new record.

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