Ruby on Rails 中的 form_for 错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17662089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:20:13  来源:igfitidea点击:

form_for error messages in Ruby on Rails

ruby-on-railsrubyruby-on-rails-4

提问by Kyle Decot

What is the preferred way to display validation error messages using form_forin Rails 4?

form_for在 Rails 4 中显示验证错误消息的首选方法是什么?

<%= form_for @post do |f| %>
  ...
<% end %>

采纳答案by Jesse Wolgamott

Same as Rails 3 -- see f.error_messages in Rails 3.0or http://railscasts.com/episodes/211-validations-in-rails-3for many different possibilities.

与 Rails 3 相同——请参阅Rails 3.0 中的 f.error_messageshttp://railscasts.com/episodes/211-validations-in-rails-3以了解许多不同的可能性。

My personal preference is to use simple_form and have it put the error next to the input.

我个人的偏好是使用 simple_form 并将错误放在输入旁边。

回答by Danny

This is how I am displaying them for my form object called @location:

这就是我为我的表单对象显示它们的方式 @location:

<% if @location.errors.any? %>
<ul>
  <% @location.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
</ul>
<% end %>

Note: put the above code after the <%= form_for @location do |f| %>line

注意:将上面的代码放在该<%= form_for @location do |f| %>行之后

回答by Wes Foster

My preferred way of doing this and keeping the code simple and DRY, is the following:

我喜欢这样做并保持代码简单和 DRY 的方法如下:

Create a new helper inside of application_helper.rb

application_helper.rb 中创建一个新的助手

# Displays object errors
def form_errors_for(object=nil)
  render('shared/form_errors', object: object) unless object.blank?
end

Create a new shared partial in shared/_form_errors.html.erb

shared/_form_errors.html.erb 中创建一个新的共享部分

<% content_for :form_errors do %>
  <p>
    <%= pluralize(object.errors.count, "error") %> prevented the form from being saved:
  </p>

  <ul>
    <% object.errors.full_messages.each do |message| %>
      <li><%= message %></li>
    <% end %>
  </ul>
<% end %>

Edit your application.html.erbfile to include the errors where you want them:

编辑您的application.html.erb文件以包含您想要的错误:

<%= yield :form_errors %>

Finally, place the helper at the start of each form:

最后,将 helper 放在每个表单的开头:

<%= form_for(@model) do |f| %>
  <%= form_errors_for @model %>

  <%# ... form fields ... %>
<% end %>

This makes it extremelysimple to manage and show your form errors across many forms.

这使得在许多表单中管理和显示表单错误变得非常简单。

回答by jlesse

I know this isn't exactly what was asked, but if you are using the simple_form gem, which I recommend, you can use f.error_notification which takes :message as an option.

我知道这并不完全是所问的问题,但是如果您使用的是我推荐的 simple_form gem,则可以使用 f.error_notification 将 :message 作为选项。

= f.error_notification message: form_errors_for(your_object)

I use a method pretty similar to Wes's answer; form_errors_for inside application_helper.rb

我使用的方法与韦斯的回答非常相似;在application_helper.rb 中的form_errors_for

def form_errors_for_base(object)
  if object.errors.messages[:base].present?
    object.errors.messages[:base].join(",\n") + "."
  else
    nil
  end
end