Forum: Ruby on Rails Multiple checkboxes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6219eab7f2fa36715baea396e5a02fb0?d=identicon&s=25 Martin Robinson (martinr)
on 2006-06-04 23:56
Please could someone suggest what might be wrong here. In my view I'm
displaying a series of check boxes and when the form is submitted I want
the controller to iterate over the values and contatenate them all  into
a string "1" if the box was checked and "0" if it wasn't.
I'm new to Ruby and Rails and I can't seem to get any values out of my
params other than "0",

Here's my code:


View:


<%= form_tag :action=>'save_question'%>
  <h3><%= @question.title %></h3>
  </br>
  <% for option in @options %>
  <input type="checkbox" name="option[]" value="<%=option.id%>"><%=
option.option_text %></br>
  <% end %>
  </br>
  <%= submit_tag%>
<%= end_form_tag%>



Controller:

def save_question

 @options = Option.find_all_by_question_id(session[:question_id])
 response = ""
 for option in @options do
   if params[:option][option.id]=="1"
     response << "1"
   else
     response << "0"
   end
 end
end
8da92d4ed91aa12535f6d870fa76b25e?d=identicon&s=25 Aaron Baldwin (Guest)
on 2006-06-05 03:11
(Received via mailing list)
I did something similar recently.  Here is the code from my controller
that
pulled out the items that were checked and saved them:

obj_keys = []
params[:objective].each_key {|k| obj_keys.push( k ) if
@params[:objective][k].to_i == 1}
@course.objectives = Objective.find( obj_keys )

Aaron
A77873df3a9766b208e009248a2a9a56?d=identicon&s=25 Hampton (Guest)
on 2006-06-05 03:20
(Received via mailing list)
Your problem is probably because of what you set "value" to be.

I think if you exam your params <%= debug params %> you will see that
the
option is returning the value that you are setting its 'checked value'
to.

Secondly, if there were 15 check boxes, they are all named the same
thing,
so as far as my understanding goes, each checkbox will overwrite itself.

May I recommend this in your view?

<%= form_tag :action=>'save_question'%>
 <h3><%= @question.title %></h3>
 </br>
 <% for option in @options %>
 <input type="checkbox" name="option[<%= option.id %>]" value="1"><%=
option.option_text %></br>
 <% end %>
 </br>
 <%= submit_tag%>
<%= end_form_tag%>

Then your controller can do this...

 @options = Option.find_all_by_question_id(session[:question_id])
 response = ""
 for option in @options do
    response << params[:options][option.id.to_s]
 end
end

Haven't tested this code, but it should work.

-hampton.
6219eab7f2fa36715baea396e5a02fb0?d=identicon&s=25 Martin Robinson (martinr)
on 2006-06-06 00:31
Hampton wrote:
> Your problem is probably because of what you set "value" to be.
>
> I think if you exam your params <%= debug params %> you will see that
> the
> option is returning the value that you are setting its 'checked value'
> to.
>
> Secondly, if there were 15 check boxes, they are all named the same
> thing,
> so as far as my understanding goes, each checkbox will overwrite itself.
>
> May I recommend this in your view?
>
> <%= form_tag :action=>'save_question'%>
>  <h3><%= @question.title %></h3>
>  </br>
>  <% for option in @options %>
>  <input type="checkbox" name="option[<%= option.id %>]" value="1"><%=
> option.option_text %></br>
>  <% end %>
>  </br>
>  <%= submit_tag%>
> <%= end_form_tag%>
>
> Then your controller can do this...
>
>  @options = Option.find_all_by_question_id(session[:question_id])
>  response = ""
>  for option in @options do
>     response << params[:options][option.id.to_s]
>  end
> end
>
> Haven't tested this code, but it should work.
>
> -hampton.

Thanks for that - I've got it working now. i took your suggestions, but
for some reason if a checkbox isn't checked a value doesn't get returned
so if I use the code

>  for option in @options do
>     response << params[:options][option.id.to_s]
>  end

I get a nil object error for unchecked checkboxes.

The actual code I got working is

  for option in @options do
    if params[:option][option.id.to_s]=="1"
     response << params[:option][option.id.to_s]
    else
     response << "0"
    end
  end

and the view code worked just as you said.
A77873df3a9766b208e009248a2a9a56?d=identicon&s=25 Hampton (Guest)
on 2006-06-06 14:20
(Received via mailing list)
Actually, I believe the helper works like this.

<input type="checkbox" name="option[<%= option.id %>]" value="1">
<input type="hidden" name="option[<%= option.id %>]" value="0">
<%= option.option_text %></br>

That should return a 0 value if unchecked.

-hampton.
This topic is locked and can not be replied to.