0

So I have multiple checkboxes on my page. I collect all of them like shown in the code below. I would like to access the values of the array before passing it on to the controller

= check_box_tag "names[]", ob.name, false, class: 'cbx'

I am able to pass them with my older code

%fieldset.actions
  = form.submit "Upgrade", :class => 'button'

Logs:

Processing by SomeController#create as HTML Parameters: {"utf8"=>"✓", "names"=>["ron", "jacob"], "commit"=>"NameButton"}

Ok. So i would like to access all values in my haml files. Is there a way before i submit my form, I can access which checkboxes are selected.

I would like to pass the names[] to my controller as a parameter.

=link_to script_name1, { :action => 'create', :names => 'dontknowhowtopassnames[]' }, :method => :post

Rails version - 3.2.17

Anish Shah
  • 17
  • 4
  • You can, with Javascript/jQuery. [example](http://stackoverflow.com/questions/9887360/check-if-checkbox-is-checked-javascript) – vich Nov 04 '15 at 22:42
  • Can you give more context? – Richard Peck Nov 04 '15 at 22:52
  • I need to pass the names[] to my controller as a parameter. I had it working with a form submit. But i had to change that logic. Now i am passing the parameters to my controller. But i don;t know how to pass in the names[] directly to my controller – Anish Shah Nov 05 '15 at 03:25
  • So what you really want is to submit the form using a link instead of a button? Because that's really trivial. – Thomas Hennes Nov 05 '15 at 04:02

1 Answers1

1

You can do that using Javascript. The exact implementation depends on what exactly you want to do with those values, but you could, for example, use the change event to track and maintain an array of all checked values :

/*
* should properly init this array if some of your checkboxes are already
* checked when the form is loaded
*/
var names = [];

$(document).ready(function() {

  $(document).on('change', '.cbx', function() {
    var name = $(this).val();
    var idx = names.indexOf(name);
    if ($(this).prop('checked') && idx === -1) {
      names.push(name);
    } else if (!($(this).prop('checked')) && idx >= 0) {
      names.splice(idx, 1);
    }
  });

});

Updated with complementary answer:

To submit a form with a link instead of a button:

In your view, replace

%fieldset.actions
  = form.submit "Upgrade", :class => 'button'

with

= link_to "Submit", "#", :class => 'submit_link'

Then in your Javascript, add the following inside the $(document).ready body:

$(document).on('click', 'a.submit_link', function(e) {
  $(this).closest('form').submit();
  e.preventDefault();
});

And that's it. You're making your life very complicated by trying to serialize the form data on your own while the form's submit() method can take care of it for you. You can serialize data on your own, and it's sometimes useful, for instance when dealing with AJAX, but in your case the default submit() is perfect for the job.

Thomas Hennes
  • 9,023
  • 3
  • 27
  • 36
  • Thanks jaxx. Is there any way i can view the values in my haml file.I need to pass the names[] to my controller as a parameter. – Anish Shah Nov 05 '15 at 03:16
  • Sorry Anish, I don't get it. You are already passing the selected names as an array to your controller, as the logs you posted show : Processing by SomeController#create as HTML Parameters: {"utf8"=>"✓", **"names"=>["ron", "jacob"]**, "commit"=>"NameButton"} – Thomas Hennes Nov 05 '15 at 03:56
  • As for viewing & updating the selected names on your page as boxes get checked/unchecked, yes, that's easily doable, if that's what you want to do. – Thomas Hennes Nov 05 '15 at 03:59
  • I know they were working with my older code.. %fieldset.actions = form.submit "Upgrade", :class => 'button' But i don't know how to pass them with my new code.. =link_to script_name1, { :action => 'create', :names => 'dontknowhowtopassnames[]' }, :method => :post – Anish Shah Nov 05 '15 at 14:29
  • See updated answer. I believe you're overthinking the whole issue. – Thomas Hennes Nov 05 '15 at 14:54