Note that there are some explanatory texts on larger screens.

plurals
  1. POrails 3 find a value in a params array
    primarykey
    data
    text
    <p>I am using jQuery UI Sortable to sort some rows on my website. </p> <p>Here is my view:</p> <pre><code>&lt;section class="data-list"&gt; &lt;article id="item_&lt;%= item.id %&gt;" class="data sortable"&gt; &lt;!-- stuff goes here --&gt; &lt;/article&gt; &lt;/section&gt; </code></pre> <p>Here is my JS:</p> <pre><code>$('.data-list').sortable({items: '&gt; .data'}).bind('sortupdate', function() { var release = getUrlVars()["id"]; $.ajax({ type: 'POST', data: $(this).sortable("serialize"), url: '/release_items/'+ release +'/prioritize' }); }); </code></pre> <p>This creates a <code>params['item']</code> array that looks like this:</p> <pre><code>["1537", "1536", "1540", "1541", "1542", "1543", "1544", "1545", "1547", "1546"] </code></pre> <p>Here is my controller code:</p> <pre><code>def prioritize @release = Release.find(params[:id]) item = @release.release_items item.each do |i| i.priority = params['item'].index(i.id.to_s).to_i + 1 i.save end end </code></pre> <p>My problem is that i have several <code>release_items</code> that are distinguished by an <code>item_type</code> column. And as it stands now i currently don't have a good way in my controller to filter by item_type in my <code>@release = Release.find(params[:id])</code> line.</p> <p>What i would like to do is make sure in the <code>item.each do |i|</code> loop that the priority is only set IF the item is in the <code>params['item']</code> array. How would i do that? Please let me know if this is not clear enough. Thanks!</p> <p><strong>EDIT 1:</strong></p> <p>For every release, there are <em>n</em> number of release items. THose release items are separated for display on the site by their <code>item_type</code> column (e.g., General, Project, Data, Patch). So on the site there are 4 different lists of items and i want to be able to sort those 4 lists individually. Like i stated above, the <code>params['item']</code> array being passed to the <code>prioritize</code> action in the controller has only the items that need to be sorted, which is want i want.</p> <p>The problem i'm running in to is that the first two lines in the <code>prioritize</code> action will get all of the release items, not just the ones in the <code>params['item']</code> array. The values in the array are the IDs of the release items that need to be sorted. Instead of getting all release items, i want to only get the items that are in the <code>params['item']</code> array. I do have a ReleaseItem model as well i can select from. So, i'm trying to do this: (i know this isn't the correct code, just for clarity sake)</p> <pre><code>item = ReleaseItem.find(conditions: "id in params['item']") </code></pre> <p>Does that make a little more sense? I appreciate your help!</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.
    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