Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 使用I18n.t提交按钮帮助程序大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想为提交按钮编写一个帮助程序,它会虑操作(创建或更新)以获得正确的翻译.他们来了 :
fr: 
  submit:
    create:
      user: "Créer mon compte"
      product: "Déposer l'objet"
      session: "Se connecter"
    update:
      user: "Mettre à jour mon compte"
      product: "Modifier l'objet"

我试过这个:

def submiT_Button(model)
  if model == nil
    I18n.t('submit.create.%{model}')
  else
    I18n.t('submit.update.%{model}')
  end
end

但它没有用,rspec发给我的是:

Capybara::ElementNotFound: Unable to find button ...

我知道这是一个语法问题,但我找不到如何使这项工作……

解决方法

你不需要帮助器,你可以使用普通导轨实现它.您唯一需要的是正确订购您的I18n YAML
fr:
  Helpers:
    submit:
      # This will be the default ones,will take effect if no other
      # are specifically defined for the models.
      create: "Créer %{model}"
      update: "Modifier %{model}"

      # Those will however take effect for all the other models below
      # for which we define a specific label.
      user:
        create: "Créer mon compte"
        update: "Mettre à jour mon compte"
      product:
        create: "Déposer l'objet"
        update: "Modifier l'objet"
      session:
        create: "Se connecter"

之后,您只需要像这样定义提交按钮:

<%= f.submit class: 'any class you want to apply' %>

Rails将获取按钮所需的标签.

你可以看到更多关于它的信息here

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – 使用I18n.t提交按钮帮助程序全部内容,希望文章能够帮你解决ruby-on-rails – 使用I18n.t提交按钮帮助程序所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。