rails 3 find a value in a params array -
i using jquery ui sortable sort rows on website.
here view:
<section class="data-list"> <article id="item_<%= item.id %>" class="data sortable"> <!-- stuff goes here --> </article> </section>
here js:
$('.data-list').sortable({items: '> .data'}).bind('sortupdate', function() { var release = geturlvars()["id"]; $.ajax({ type: 'post', data: $(this).sortable("serialize"), url: '/release_items/'+ release +'/prioritize' }); });
this creates params['item']
array looks this:
["1537", "1536", "1540", "1541", "1542", "1543", "1544", "1545", "1547", "1546"]
here controller code:
def prioritize @release = release.find(params[:id]) item = @release.release_items item.each |i| i.priority = params['item'].index(i.id.to_s).to_i + 1 i.save end end
my problem have several release_items
distinguished item_type
column. , stands don't have way in controller filter item_type in @release = release.find(params[:id])
line.
what make sure in item.each |i|
loop priority set if item in params['item']
array. how that? please let me know if not clear enough. thanks!
edit 1:
for every release, there n number of release items. release items separated display on site item_type
column (e.g., general, project, data, patch). on site there 4 different lists of items , want able sort 4 lists individually. stated above, params['item']
array being passed prioritize
action in controller has items need sorted, want want.
the problem i'm running in first 2 lines in prioritize
action of release items, not ones in params['item']
array. values in array ids of release items need sorted. instead of getting release items, want items in params['item']
array. have releaseitem model can select from. so, i'm trying this: (i know isn't correct code, clarity sake)
item = releaseitem.find(conditions: "id in params['item']")
does make little more sense? appreciate help!
i think understand trying do...
def prioritize @release = release.find(params[:id]) items = @release.release_items.where("id in (?)", params['item']) items.each_with_index |item, index| item.priority = index + 1 item.save end end
Comments
Post a Comment