I’m passing the below information through parameter from view to controller
parameters:{"Something"=>{"a" => "1", "b" => "0", "c" => "1", "d" => "0" #and so on}}
I want to access all the characters that have “1” as their value and concatenate into the string.
I tried
Something.each do |key, value|
if(value == "1")
string = string + key
end
end
It is throwing error saying that it could not execute nil.each and that i might be expecting an array.
It appears to me that Something is a hash and in turn has some hashes in it.
So i initialised Something to
Something = Hash.new { |Something, k| Something[k] = Hash.new }
But i still get the same error.
Just work with the
paramshash. This should do what you need:selectfilters the params to only those with the value"1"keysreturns an array with all the keys in the hashreducejoins all elements with a concat operation (+)Edit
To concatenate and add the “Extra” word:
For each parameter:
Only to the extra parameters, but not to the first one:
See more information on
injecthere.