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

35 views
Skip to first unread message

Victor Ab

unread,
Aug 6, 2014, 12:03:18 PM8/6/14
to rubyonra...@googlegroups.com
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,

--
Posted via http://www.ruby-forum.com/.

Pardeep Dhingra

unread,
Aug 7, 2014, 7:52:47 AM8/7/14
to rubyonra...@googlegroups.com
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.

Victor Ab

unread,
Aug 7, 2014, 8:22:47 AM8/7/14
to rubyonra...@googlegroups.com
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?

Pardeep Dhingra

unread,
Aug 7, 2014, 8:34:13 AM8/7/14
to rubyonra...@googlegroups.com
can i see you code in skype or somewhere. I want to troubleshoot it.

Victor Ab

unread,
Aug 7, 2014, 8:38:03 AM8/7/14
to rubyonra...@googlegroups.com
I will send you a private message in a few minutes. Thanks.

Victor Ab

unread,
Aug 7, 2014, 8:54:57 AM8/7/14
to rubyonra...@googlegroups.com
Sent

Victor Ab

unread,
Aug 7, 2014, 11:17:00 AM8/7/14
to rubyonra...@googlegroups.com
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?

Pardeep Dhingra

unread,
Aug 7, 2014, 11:20:00 AM8/7/14
to rubyonra...@googlegroups.com
<%= 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.

Victor Ab

unread,
Aug 7, 2014, 11:42:01 AM8/7/14
to rubyonra...@googlegroups.com
Ok, it seems it works! Thanks!
Reply all
Reply to author
Forward
0 new messages