rails form checkbox array/hash value
How can I create a checkbox that stores a ha开发者_如何学编程sh, so that when I retrieve the value in params array I get a hash.
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.
精彩评论