Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to parse values in multidimensional array and select one over the other based on condition?
    primarykey
    data
    text
    <p>I have an array that behaves like a multidimensional array through spaces, like:</p> <p>"roles"=>["1 editor 0", "1 editor 1", "2 editor 0", "2 editor 1", "14 editor 0", "15 editor 0"], "commit"=>"Give Access", "id"=>"3"}</p> <p>Each array value represents [category_id, user.title, checked_boolean], and comes from </p> <pre><code>form &lt;%= hidden_field_tag "roles[]", [c.id, "editor", 0] %&gt; &lt;%= check_box_tag "roles[]", [c.id, "editor", 1 ], !!checked %&gt; </code></pre> <p>which I process it using splits</p> <pre><code>params[:roles].each do |role| cat_id = role[0].split(" ")[0] title = role.split(" ")[1] checked_boolean = role.split(" ")[2] end </code></pre> <p>Given the array at the top, you can see that the "Category 1" &amp; "Category 2" is checked, while "Cat 14" and "Cat 15" are not. </p> <p>I would like to compare the values of the given array, and if both 1 &amp; 0 exists for a given category_id, I would like to get rid of the value with "checked_boolean = 0". This way, if the boolean is a 1, I can check to see if the Role already exists, and if not, create it. And if it is 0, I can check to see if Role exists, and if it does, delete it.</p> <p>How would I be able to do this? I thought of doing something like params[:roles].uniq but didn't know how to process the uniq only on the first split.</p> <p>Or is there a better way of posting the "unchecks" in Rails? I've found solutions for processing the uncheck action for simple checkboxes that passes in either true/false, but my case is different because it needs to pass in true/false in addition to the User.Title</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload