1

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

1
  • Ah, sorry I am kinda new, didnt realize that feature was available. Will do! Commented Jun 4, 2010 at 19:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.