简体   繁体   English

Rails:如何在没有模型名称,关联名称的情况下显示错误消息

[英]Rails: How to display error message without model name, association name

I'd like to display error message without model name, association name. 我想显示错误消息,不带型号名称,关联名称。

For example, the following error was displayed, 例如,显示以下错误,

Rooms events base To time must be after from tim

But I'd like to display only To time must be after from time . 但是我只想显示To time must be after from time

The validate in my model is as followings; 我的模型中的验证如下:

validate do |e|
  if e.start_at.present? && e.end_at.present? and e.start_at > e.end_at
    errors[:base] << "To time must be after from time"
  end
end

It would be appreciated if you could give me how to display only To time must be after from time . 如果您能给我如何仅显示的信息,将不胜感激To time must be after from time

application.html.erb application.html.erb

    <% flash.each do |message_type, message| %>
      <%= content_tag(:div, message, class: "alert alert-#{message_type}") %>
    <% end %>

SOLVED! 解决了!

It works when I set the followings in en.yml 当我在en.yml中设置以下内容时,它可以工作

en:
  errors:
    format: "%{message}"

Add the following in your application.html above yield 在yield上方的application.html中添加以下内容

<% flash.each do |key, value| %>
      <div class="alert alert-<%= key %>"><%= value %></div>
<% end %>

errors is a hash. errors是哈希。 The key is the name of the field name or :base and the value is an array of your error messages as strings. 关键字是字段名或:base名称,值是错误消息的字符串数组。

values on a hash will return an array of the values without the keys. values上的散列将返回值的阵列没有密钥。 Because the value in this hash is an array, we end up with an array of arrays: [['To time must be after from time']] 因为此哈希中的值是一个数组,所以我们得到一个数组数组: [['To time must be after from time']]

I use flatten to squash all of the nested arrays to a single top level array. 我使用flatten将所有嵌套数组压缩为单个顶级数组。

join("\\n") will join all of the array elements into a single string, separated by a newline character. join("\\n")将所有数组元素合并为一个字符串,并用换行符分隔。

Put it all together and you get something like this: 将所有内容放在一起,您将得到以下内容:

flash[:error] = @object.errors.values.flatten.join("\n")

http://api.rubyonrails.org/classes/ActiveModel/Errors.html#method-i-values http://api.rubyonrails.org/classes/ActiveModel/Errors.html#method-i-values

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 简单形式的宝石-如何在关联中显示模型的名称-Rails 4 - Simple Form Gem - How to display the name of a model in an association - Rails 4 在关联错误消息中隐藏/删除模型名称 - Hide/remove model name in association error message 如何在附加文件名上的rails3中更改模型的错误消息 - How to change error message of model in rails3 on attach file name rails方法获取模型的关联名称 - rails method to get the association name of a model 如何在导轨中的模型上显示错误消息 - How to display error message on model in rails 如何让 Rails 关联使用某个名称来访问不同名称的模型? - How do I get a Rails association to use a certain name to access a model of a different name? Rails 4-从错误消息中删除属性名称以关联模型 - Rails 4 - Remove attribute name from error message for associated model presence Rails 4:如何在f.label中使用道具名称显示错误消息 - Rails 4: How to display error message using a prop name in f.label 如何遍历嵌套的关联而不在关联中输入每个模型的导轨 - How to traverse a nested association without typing every model in the association in rails Rails如何获取关联上的特定错误消息 - Rails how to get a particular error message on association
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM