2012-06-02 59 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