Question

I am getting this in my error list :

@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."]
                  }

Here I want to remove the value "can't be blank" value from this hash so that I will get the validation error messages which was included by me.

is it possible to remove "can't be blank" value from above hash list and I will get this in result :

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

How to remove a specific value from a hash list(want to remove a specific value not a complete key,value pair).

Was it helpful?

Solution

Yes, possible.

@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."]}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top