-2

我正在使用 Zurb Foundation 和 Simple form

<%= simple_form_for @complaint do |f| %>
   <%= f.error_notification %>

   <%= f.association :company, as: :radio, label: false %>
   <%= f.input :country, priority: ["United States"] %>
   <%= f.input :city %>
   <%= f.input :client, placeholder: 'Coca-Cola' %>
   <%= f.input :body %>

   <%= f.button :submit %>
<% end %>

在我的模型中:

class Complaint < ActiveRecord::Base
  belongs_to :company
  attr_accessible :body, :city, :client, :country, :company_id
  validates :company, presence: { message: 'Company cannot be blank!' }
  validates :body, presence: true
  validates :country, presence: true
  validates :city, presence: true
end

当我在空表单上单击提交时,我预计会出现“公司不能为空白!”的错误。等等

我使用<%= f.error_notification %>不正确吗?我怎样才能到那里显示错误?

4

2 回答 2

0

您能否显示您的控制器创建操作代码。我有一个类似的错误,问题就在那里。

您可能使用过@complaint.save!而不仅仅是@complaint.save

您应该调用 save 而不是 save!因此 if 语句可能会失败,您的对象将在其错误对象中包含消息,并且 simple_form 可以在 render :new 之后将它们呈现在每个表单输入旁边。

于 2014-01-31T11:20:02.867 回答
0

不要将其视为逐字逐句,因为我自己只有几个月的时间进入 Rails,但我不认为 error_notification 旨在显示错误:- 错误本身旨在与组件一起显示。但是我有一种情况,使用 f.input_field 组件旁边的 f.error 字段很乱,所以我希望我的所有错误都在页面顶部,所以我创建了一个简单的部分:

-if object.errors.any?
  .alert{:class => "alert-error"}
    %p= "Please correct the following #{pluralize(object.errors.count, 'error')}:"
    %ul
      - object.errors.full_messages.each do |msg|
        %li= msg

并将其呈现在页面顶部:

= render "layouts/errors", :object => @booking

对不起,哈姆。这

  .alert{:class => "alert-error"} 

它的样式与通知相同。

于 2013-07-17T18:00:05.380 回答