我在我的错误列表中找到了这个:

@error_messages = {
                   :password=>["can't be blank", "Password is required."],
                   :"addresses.firstname"=>["can't be blank","Firstname is required."],
                   :"addresses.city"=>["can't be blank", "city is required."]
                  }

在这里,我想从这个散列中删除“不能为空”值,这样我就可以得到我包含的验证错误消息。
是否可以从上面的哈希列表中删除“不能为空”值,我将得到以下结果:
       @error_messages = {
                          :password=>["Password is required."],
                          :"addresses.firstname"=>["Firstname is required."],
                          :"addresses.city"=>["city is required."]
                         }

如何从哈希列表中移除特定值(要移除特定值而不是完整的键、值对)。

最佳答案

是的,有可能

@error_messages = {
                   :password=>["can't be blank", "Password is required."],
                   :"addresses.firstname"=>["can't be blank","Firstname is required."],
                   :"addresses.city"=>["can't be blank", "city is required."]
                  }

@error_messages.each do |_,v|
   v.delete( "can't be blank" )
end

@error_messages
# => {:password=>["Password is required."],
#     :"addresses.firstname"=>["Firstname is required."],
#     :"addresses.city"=>["city is required."]}

关于ruby - 如何从存储在 ruby 哈希中的数组中删除特定值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22489385/

10-09 02:59