在Active Admin中显示一对多关系表的错误信息

18

我在Active Admin中遇到了一个显示错误消息的问题。

我能够在表单字段中显示所有的错误消息。 但在下面的代码中,我需要至少添加一项技能和最多5项技能。 否则需要抛出一个错误消息。

我在模型中添加了一个验证:

validates :skills, :length => { :minimum => 1, :maximum => 5, :message => " 至少应该为1,最多不超过5"}

这个验证完全符合要求,但没有显示任何错误消息。

有谁能帮我解决错误消息的显示问题吗?

以下是代码:

form :html => { :enctype => "multipart/form-data" } do |f|

    f.inputs "User", :multipart => true do

        f.input :name
        f.input :email,  :as => :email
        f.input :profile_name
        f.input :date_of_birth
        f.input :gender,  :as => :select, :collection => Gender::GENDERS
      end
      f.inputs "Skills* ( minimum 1 & maximum 5 )" do
        f.has_many :skills do |p|
          if !p.object.nil?
            # show the destroy checkbox only if it is an existing appointment
            # else, there's already dynamic JS to add / remove new appointments
            p.input :_destroy, :as => :boolean, :label => "Destroy?",
                    :hint => "Check this checkbox, if you want to delete this field."
          end
          p.input :description
          p.input :title
        end
      end
    end
  end
2个回答

39

activeadmin 0.5.1可以在GitHub上获取。变更日志中包含以下内容:

"通过@robdiciuccio添加语义错误支持#905"

此功能的拉取请求在这里

例子

form do |f|
  f.semantic_errors *f.object.errors.keys
  f.inputs
  f.inputs "Locations" do
    f.has_many :locations do |loc|
      loc.input :address
      loc.input :_destroy, :as => :boolean, :label => "Delete"
    end
  end
  f.buttons
end

要使用它,请将其添加到Gemfile中

gem 'activeadmin', :git =>  "git://github.com/gregbell/active_admin.git", :tag => "v0.5.1"

这个有帮助.. 谢谢 @Fivell - Piyush Choudhary
根据需要在以下模块下定制“semantic_errors” Formtastic :: Helpers :: ErrorsHelper现在按要求工作。谢谢@Fivell - Piyush Choudhary

0

为了通过验证,请尝试这样做

validates_length_of :skills,
  :within => 1..5,
  :too_short => 'too short message',
  :too_long => 'too long message'

嗨 Dipak,谢谢。 就有效性而言,这个工作得很好。 只是想知道,我们是否可以在 UI(表单)中使用默认的 activeadmin 流来显示它。 - Piyush Choudhary

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接