2012-06-02 113 views
0

在html中,我有2個使用.column類的div。所以,我創建了一個每個循環來序列化每個.column div的數據。使用rails處理雙重post請求

// item.js 
$('.column').each(function(){ 
    update: $.post($(this).data('update-url'), $(this).sortable('serialize')); 
}); 

實例是item[]=1&item[]=5&item[]=4item[]=2&item[]=3。這些參數將被髮送到rails控制器,通過POST進行排序。

雖然在軌控制器

def sort 
    params[:item].each_with_index do |id, index| 
    Item.where(_id: id.last).update(position: index+1) 
    #Item.where(_id: id.last).update(position: index+4) 
    end 
    render nothing: true 
end 

問:如何處理在乾燥方法後的參數?對於第一個請求(item[]=1&item[]=5&item[]=4),我想用position: index+1更新數據庫(mongoid),而第二個請求我想用position: index+4更新。謝謝。

+0

一個新的參數(例如'request_type')還是有兩個不同的動作? –

+0

它的作品,但它會是醜陋的? – shinnyx

+0

說實話,你在做什麼看起來有點醜陋。 :)您也可以將增量作爲參數發送(1或4)。 –

回答

0

您可以將增量值作爲表單中的參數發送。在你看來,只需添加一個新的隱藏代碼,請form標籤內(假設你使用ERB):

<%= hidden_field_tag :increment, [INCREMENT_VALUE] %> 

和編輯您sort的方法來看看你有沒有考慮加入類似

def sort 
    params[:item].each_with_index do |id, index| 
    Item.where(_id: id.last).update(position: index+1 + params[:increment].to_i) 
    end 
    render nothing: true 
end