【问题标题】:Showing error messages in active admin for has many relationship table在活动管理员中显示错误消息有许多关系表
【发布时间】:2012-11-15 09:59:06
【问题描述】:

我在活动管理员中显示错误消息时遇到问题。

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

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

验证 :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

【问题讨论】:

    标签: ruby-on-rails activeadmin


    【解决方案1】:

    activeadmin 0.5.1 在 github 上可用。 它包含更新日志中的下一行

    “添加对@robdiciuccio 的语义错误 #905 的支持”

    这是具有此功能的拉取请求 https://github.com/gregbell/active_admin/pull/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
    • 自定义 'semantic_errors' 可在以下模块下进行更多自定义 Formtastic::Helpers::ErrorsHelper 现在按要求工作。谢谢@Fivell
    【解决方案2】:

    为了通过验证试试这个

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

    【讨论】:

    • 嘿迪帕克,谢谢。就验证而言,这个工作正常。只是想知道,如果我们可以使用默认的 activeadmin 流程在 UI(表单)中显示它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多