check_box_tag problems

Hi, I'm having some trouble with my form. The thing is I'm using a loop to generate check_box_tags and then when the user clicks submit I want to be able to see what he checked. For each checked box I want to copy a file to the corresponding directory.

Here's the form:

<%form_for :cats, :url => {:action => :save_mt} do%>   <%@flokkar.each do |flokkur|%>   <p>     <%= check_box_tag("cats[#{flokkur}]",1,false) %>     <%= flokkur %>   </p>   <%end%>   <%= submit_tag "Vista mælitæki", :class => 'submit'%> <%end%>

Development.log shows these parameters have been passed:

Parameters: {"commit"=>"Vista mælitæki", "authenticity_token"=>"B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0=", "cats"=>{"Geðsjúkraþjálfun"=>"1"}}

Here I only checked "Geðsjúkraþjálfun" and then submitted.

The problem is in the controller. How can I loop through the cats hash and check if the key is there? I now have something like this:

@flokkar.each do |flokkur|       if params[:cats][:flokkur] == 1           ....do some stuff...       end end

#(@flokkar is the array that I used to build the form.)

But i never execute the block inside the if condition check. I've also tried params[:cats][flokkur] but nothing happens. How can I make ruby check whether a key exists? Like if I had a string "foo" and a hash "durr", how could I check if durr[:foo] exists without writing the whole thing??

BR, Sindri

Hi,

Are you trying to apply a loop on cats hash??

If yes then try this:

@var1 = params[:cats].each do |key, value|   @var1 << key end

if @var1.include?(your_key)   do this else   do that end

Regards,

Saurabh

Sindri Guðmundsson wrote:

Hi, I'm having some trouble with my form. The thing is I'm using a loop to generate check_box_tags and then when the user clicks submit I want to be able to see what he checked. For each checked box I want to copy a file to the corresponding directory.

Here's the form:

<%form_for :cats, :url => {:action => :save_mt} do%>   <%@flokkar.each do |flokkur|%>   <p>     <%= check_box_tag("cats[#{flokkur}]",1,false) %>     <%= flokkur %>   </p>   <%end%>   <%= submit_tag "Vista mælitæki", :class => 'submit'%> <%end%>

[...]

This is strange code. You're inside a form_for, so why are you using check_box_tag rather than check_box ?

Best,

Marnen Laibow-Koser wrote:

[...]

This is strange code. You're inside a form_for, so why are you using check_box_tag rather than check_box ?

I read somewhere that I shouldn't do it like this, but I couldn't get the info on checked/uncecked using check_box. Should I get rid of the form_for and change the submit button to POST button_to? Why?

Thanks Saurabh Peshkar, ofc I should've done that, thanks! :slight_smile:

Sindri Guðmundsson wrote:

Marnen Laibow-Koser wrote:

[...]

This is strange code. You're inside a form_for, so why are you using check_box_tag rather than check_box ?

I read somewhere that I shouldn't do it like this, but I couldn't get the info on checked/uncecked using check_box. Should I get rid of the form_for and change the submit button to POST button_to? Why?

No, you should fix the problem that's preventing you from using check_box!

Thanks Saurabh Peshkar, ofc I should've done that, thanks! :slight_smile:

Best,

Marnen Laibow-Koser wrote:

Sindri Guðmundsson wrote:

Marnen Laibow-Koser wrote:

[...]

This is strange code. You're inside a form_for, so why are you using check_box_tag rather than check_box ?

I read somewhere that I shouldn't do it like this, but I couldn't get the info on checked/uncecked using check_box. Should I get rid of the form_for and change the submit button to POST button_to? Why?

No, you should fix the problem that's preventing you from using check_box!

Thanks Saurabh Peshkar, ofc I should've done that, thanks! :slight_smile:

Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org

Hi, I've tried that, but I can't seem to have check_box write anything else to params[:cats] other than nil. The only thing that happened was that params[:cats] was an array the same length as the number of categories I had but only contained nil-objects. Could you please show me how I should generate the check_boxes inside the loop in my view?

BR, Sindri

If this helps these are the parameters passed when I use check_box:

  Parameters: {"commit"=>"Vista mælitæki", "authenticity_token"=>"B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0=", "cats"=>{"flokkur"=>[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]}}

Sindri Guðmundsson wrote:

If this helps these are the parameters passed when I use check_box:

  Parameters: {"commit"=>"Vista mælitæki", "authenticity_token"=>"B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0=", "cats"=>{"flokkur"=>[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]}}

And what does that version of the view code look like?

Best,

Marnen Laibow-Koser wrote:

Sindri Guðmundsson wrote:

If this helps these are the parameters passed when I use check_box:

  Parameters: {"commit"=>"Vista mælitæki", "authenticity_token"=>"B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0=", "cats"=>{"flokkur"=>[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]}}

And what does that version of the view code look like?

Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org

Hi, It looks like this now;

<%form_for :cats, :url => {:action => :save_mt} do |form|%>   <%@flokkar.each do |flokkur|%>   <p>     <%= form.check_box 'flokkur' %>     <%= flokkur %>   </p>   <%end%>   <%= submit_tag "Vista mælitæki", :class => 'submit'%> <%end%>

I've tried different parameters for the check_box but to no success...

BR, Sindri

Sindri Guðmundsson wrote:

Marnen Laibow-Koser wrote:

Sindri Guðmundsson wrote:

If this helps these are the parameters passed when I use check_box:

  Parameters: {"commit"=>"Vista mælitæki", "authenticity_token"=>"B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0=", "cats"=>{"flokkur"=>[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]}}

And what does that version of the view code look like?

Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org

Hi, It looks like this now;

<%form_for :cats, :url => {:action => :save_mt} do |form|%>   <%@flokkar.each do |flokkur|%>   <p>     <%= form.check_box 'flokkur' %>     <%= flokkur %>   </p>   <%end%>   <%= submit_tag "Vista mælitæki", :class => 'submit'%> <%end%>

I've tried different parameters for the check_box but to no success...

BR, Sindri

Finally got it, the empty hash did the trick :wink: Thanks though :slight_smile:

<%form_for :cats, :url => {:action => :save_mt} do |form|%>   <%@flokkar.each do |flokkur|%>   <p>     <%= form.check_box flokkur, {}, "yes", "no" %>     <%= flokkur %>   </p>   <%end%>   <%= submit_tag "Vista mælitæki", :class => 'submit'%> <%end%>