How can I create a checkbox that stores a hash, so that when I retrieve the value in params array I get a hash.
A:
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.
Matt S
2010-06-04 19:23:01
well when I do this..<% result.each do |result, result_options|%> <% result_options.each do |option, count| %> <%= f.check_box "facet", {result => option}%> <% end %><%end %>I get..<input type="checkbox" value="1" name="search[facet]" id="search_facet" article_tag="Energy">the value is still 1
badnaam
2010-06-04 19:30:13
Try `check_box :search, :facet, {}, hash` Do not use the f.check_box and match :search to whatever the part before the `[]` in the `name="name[]"` is
Matt S
2010-06-04 19:53:09
Thanks.Here is what I get<input type="checkbox" value="article_tagEnergy" onclick="refreshResults(this);" name="search[conditions]" id="search_conditions">From.. <%= check_box :search, "conditions", {:onclick => "refreshResults(this);"}, {result => option} %>Instead of a hash the {result => option} is producing article_tagEnergy.In my controller I am hoping to get params[:search][:conditions] = {{articletag => Energy}, {city => "xxx"}}
badnaam
2010-06-04 21:11:08
AH! When i hear 'hash' I think of a 'hash code' (MD5, SHA1, etc). What is it you want in the checkbox value EXACTLY? The only thing you can put in there are effectively strings. You'll need to do some post processing to determine it. I'm going to add another answer to format this better.
Matt S
2010-06-04 21:30:53
Well I thought you could send arrays and hashes in params?I want something like this in my search model.params = { :controller => 'search', :action => 'set_search', :conditions => {:city => [:x, :y], :tag => [a, b]} :models => {[abc], [dddd]} }is that possible? I }
badnaam
2010-06-04 21:44:40
Doing that causes the params[:conditions] to be "0" in the controller.Since there are multiple checkboxes with conditions name? May be today isn't my day, this should not be this hard :).. here is my entire view.Look at line 62. http://pastie.org/992456I am trying to create check boxes so that when a user click on article_tag facet, I retrievve records for those facets only, user might select multiple facets that translate into multiple conditions.
badnaam
2010-06-04 22:07:31