Issue in exporting checkbox values (in list) from view to controller

Hello all,

my skills in Ruby are very limited so sorry if my questions seem stupid.
I’ve made some searchs on Google and made many tries but I still have
problems. Hope you will able to help me.

Here is the main goals of what I want to do:

I open a page (view).
On this page, some informations are displayed about users.
For each user, I have two check-boxes.

What I want : If a user wants to check / uncheck specific check-box,
send the new “values” to a controller for treatments using a button
(similar to submit_tag).

In my view, I use two check-box tags as following:

<% @users.each do |user| %>

    ...

    <% check_box_tag "scope[]", user.uid, user.in_scope %>
    <% check_box_tag "state[]", user.uid, user.in_scope %>

    ...

<% end %>

Need to precise:

  • scope[] and state[] are not “declared” anywhere else than in this
    view. I’m expecting to retrieve two lists similar to:

“scope”=> [“uid_1” => “true” (or 1), “uid_2” => “false” (or 0), …]
“state”=> [“uid_1” => “true” (or 1), “uid_2” => “false” (or 0), …]

  • user.uid is a specific id for each user,
  • user.in_scope is a boolean (true by default).

At the beginning of the view, I also have:

<%= button_to(“Update’ informations”, {:controller => ‘myController’,
:action => ‘myAction’, :id => params[:id]}, class: “myClass”) %>

to make the treatment using a specific controller (“myController”).

In “myController”, I try to retrieve the values of check-boxes using :

list_one = params[:scope]
list_two = params[:stats]

but both list are always equal to ‘nil’.

I’m sure I’m missing some things but I don’t know what.

If you have any solutions, please could you precise:

  • If I have to declare specific variables and where,
  • If I have to make database migration or something similar (and where),
  • If I have to modify my previous code, how (with maximum informations
    please).

Any help will be really appreciate!!!

Thank you very much!

Best regards,

Hi,

http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag#182-Delete-collections-with-check-box-tags

Delete collections with check box tags

This example may help you,this is quite similar to your problem.

Thanks for the reply pardeep_d.

Unfortunately, it seems it doesn’t work. In my controller, the two lists
are still equal to nill…

The example doesn’t provide enough details from my point of view. Maybe
I miss something?

I will send you a private message in a few minutes. Thanks.

can i see your code in skype or somewhere. I want to troubleshoot it.

Sent

<%= form_tag({controller: “myController”, action: “myAction”}, method:
“post”) %>

<% @users.each do |user| %>
<%= fields_for user do |us| %>
<%= counter %> <% counter += 1 %>
<%= user.firstname %>
<%= user.lastname %>
<%= user.email_address %>
<%= user.uid %>
<%= user.sent %>
<%= check_box_tag “scope[]”, user.uid, user.in_scope %>
<%= check_box_tag “stats[]”, user.uid, user.in_stats %>
<% end %>
<% end %>

<%= submit_tag %>
<%end%>

I think this will not create multiple submit button.

Ok, it seems it works! Thanks!

Thanks for you help pardeep_d.

I get some results using “form_tag” and a “submit_tag” like:

<%= form_tag({controller: “myController”, action: “myAction”}, method:
“post”) %>

   [Check-box code]

<%= submit_tag %>
<% end %>

Unfortunately, there are still issues:

  • First, if I use “submit_tag”, as user’s information are displayed from
    a loop, I will have many buttons.

If I click on one of them, I get only ONE result, linked to the specific
user.

  • If I use button_to instead of submit_tag, then I get no results (“nil”
    for both lists).

Any ideas please?