badnaam
badnaam

Reputation: 1884

rails form checkbox array/hash value

How can I create a checkbox that stores a hash, so that when I retrieve the value in params array I get a hash.

Upvotes: 1

Views: 6792

Answers (1)

Matt S
Matt S

Reputation: 1757

In your controller @hash = [your hash code]

In your view: <% check_box_tag 'name', @hash %>

Use the other view helpers if you want to make it part of a form http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M002256 and http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

EDIT: Based on the comments below

You'll need to do things a little differently. Build your checkbox like this: <%= check_box :search, "conditions", {:onclick => "refreshResults(this);"}, "#{result.to_s}=#{option.to_s}" %>

This will produce checkboxes where value="city=blah blah", when you process this in rails do:

search = {}
conditions.each do |c|
    c.split('=').each{|k,v| search[k] = v}
end

You can then use your search hash to filter.

Upvotes: 1

Related Questions