加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用I18n.t提交按钮帮助程序

发布时间:2020-12-16 20:48:49 所属栏目:百科 来源:网络整理
导读:我想为提交按钮编写一个帮助程序,它会考虑操作(创建或更新)以获得正确的翻译.他们来了 : fr: submit: create: user: "Créer mon compte" product: "Déposer l'objet" session: "Se connecter" update: user: "Mettre à jour mon compte" product: "Modif
我想为提交按钮编写一个帮助程序,它会考虑操作(创建或更新)以获得正确的翻译.他们来了 :
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读