Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails如何从表单对象本地化属性_Ruby On Rails_Forms_Activerecord_Internationalization - Fatal编程技术网

Ruby on rails Rails如何从表单对象本地化属性

Ruby on rails Rails如何从表单对象本地化属性,ruby-on-rails,forms,activerecord,internationalization,Ruby On Rails,Forms,Activerecord,Internationalization,我已经做了一些研究,但没有找到如何定位表单对象的属性 以下是我的表单对象模型的相关部分: class Pledge include ActiveModel::Validations include ActiveModel::Conversion include ActiveAttr::Attributes extend ActiveModel::Naming attribute :pledge_amount attribute :reward_id attribut

我已经做了一些研究,但没有找到如何定位表单对象的属性

以下是我的表单对象模型的相关部分:

class Pledge
  include ActiveModel::Validations
  include ActiveModel::Conversion
  include ActiveAttr::Attributes
  extend ActiveModel::Naming

  attribute :pledge_amount
  attribute :reward_id
  attribute :message

  validates :message, presence: true, length: { maximum: 140 }
  validates :reward_id, presence: true
  validates :pledge_amount, presence: true

  ...
end
以及我的区域设置文件/en.yml:

en:
  activerecord:
    models:
      pledge: "Pledge"
    attributes:
      pledge:
        pledge_amount: "Pledge Amount"
        reward_id: "Reward"
        message: "Message"
  helpers:
    submit:
      pledge:
        create: "Next Step"
    label:
      pledge:
        pledge_amount: "Enter your pledge amount"
        reward_id: "Select your reward"
        message: "Write a Support Message"
通过此设置,我成功地本地化了表单上的标签,以下是表单代码的一部分:

<%= form_for @pledge do |f|  %>
  <% if @pledge.errors.any? %>
    <div id="error_explanation">
      <h2><%= t :not_saved, count: @pledge.errors.count, model: t(:pledge) %></h2>
      <ul>
        <% @pledge.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  ...
  <%= f.label :pledge_amount %>
  <%= f.text_field :pledge_amount %>
  ...
  <%= f.submit class: "btn btn-lg btn-primary" %>
<% end %>
当它应该呈现:

one error prohibited this Pledge from being saved:
 - Pledge Amount can't be blank
如果您对这条消息特别好奇,以下是代码,简称:

错误/en.yml

en:
  not_saved:
    one: "one error prohibited this %{model} from being saved:"
    other: "%{count} errors prohibited this %{model} from being saved:"
  errors:
    format: "%{message}"
  activerecord:
    errors:
      messages:
        blank: "%{attribute} can't be blank"
这适用于我的所有其他模型,但对于这一个,由于某种原因它不起作用,我认为我在我的requests/en.yml文件上使用了错误的键,因为我的requestment模型没有从ActiveRecord继承


有人知道实现这一点的正确方法吗?

我通过在表单对象中的每个验证中添加自定义消息,部分解决了这一问题,如下所示:

one error prohibited this Pledge from being saved:
 - can't be blank
validates :pledge_amount, :presence => { :message => I18n.t(:blank_pledge_amount) }
validates_presence_of :message, :message => I18n.t(:blank_message)
validates_length_of :message, :maximum => 140, message: I18n.t(:message_too_long)
还有我的en.yml文件

blank_pledge_amount: "Please insert a pledge amount"
这样做很好,但我仍然想知道一个更好的解决方案,如果有人有一个更好的。 我希望这能帮助其他有同样问题的人

为了验证message属性的存在性和长度,我对这两个属性的消息执行了以下操作:

one error prohibited this Pledge from being saved:
 - can't be blank
validates :pledge_amount, :presence => { :message => I18n.t(:blank_pledge_amount) }
validates_presence_of :message, :message => I18n.t(:blank_message)
validates_length_of :message, :maximum => 140, message: I18n.t(:message_too_long)

将翻译放在yaml中的
activemodel
上,而不是
activerecord

en:
  activemodel:
    models:
      pledge: "Pledge"
    attributes:
      pledge:
        pledge_amount: "Pledge Amount"
        reward_id: "Reward"
        message: "Message"

我是从一家报纸的评论中找到这个答案的。查看Greg Yardley的评论。

这似乎有效,但我确实喜欢(对于另一个模型):en:activemodel:errors:models:message:attributes:name:blank:“请插入一个名称…”但是atributes没有翻译,它显示的是“不能为空”而不是“message不能为空”,但我在这里粘贴了这种方法,它可以翻译。这应该是选择的答案。