Active Record Validation错误显示在View中的表单之外

时间:2016-08-21 16:42:47

标签: ruby-on-rails ruby

Active Record Validation Errors显示在表单外部时,它们在浏览器顶部显示如下:

{
"title"=>["is too long (maximum is 25 characters)"],
"picture"=>["can't be blank"],
"tag_list"=>["5 tags maximum"]
}

如何将这个阵列变成更具苦行的东西?

2 个答案:

答案 0 :(得分:0)

因为您没有使用simple_for

答案 1 :(得分:0)

它很容易用红宝石。

error_hash = {
    "title"=>["is too long (maximum is 25 characters)"],
    "picture"=>["can't be blank"],
    "tag_list"=>["5 tags maximum"]
}

req_hash = {}
error_hash.each do |_hash|
  req_hash[_hash[0]] = _hash[1][0]
end

p req_hash
#=> {"title"=>"is too long (maximum is 25 characters)", "picture"=>"can't be blank", "tag_list"=>"5 tags maximum"}

尽管有很多简洁的方法可以做。

希望这对你有帮助!