[英]How to post javascript data to a rails controller action?
I am using jQuery Shapeshift for drag and drop ordering for some lists that i have. 我正在使用jQuery Shapeshift对某些列表进行拖放排序。 All i need is to send or post this data below to my rails controller action so i can update the order. 我需要做的就是将下面的数据发送或发布到我的rails controller动作中,以便我可以更新订单。
This is what i get in my console each time i drag a list. 这是我每次拖动列表时都会在控制台中得到的。
list_46
0
list_45
1
list_38
2
list_44
3
list_39
4
list_37
5
This is the exact path that i need to send that data above to. 这是我需要将上述数据发送到的确切路径。 I have my routes setup correctly. 我的路线设置正确。
sortlists_boards POST /boards/sortlists(.:format)
Javascript Code JavaScript代码
jQuery(function() {
$('.listwrap').shapeshift();
return $('.listwrap').on('ss-rearranged', function(e) {
$(this).children().each(function() {
#I need to send/post these two lines below to sortlists_boards_path
console.log($(this).attr("id"))
console.log($(this).index())
});
});
});
Some Github issues that might help 一些Github问题可能会有所帮助
https://github.com/McPants/jquery.shapeshift/issues/64 https://github.com/McPants/jquery.shapeshift/issues/64
https://github.com/McPants/jquery.shapeshift/issues/88 https://github.com/McPants/jquery.shapeshift/issues/88
https://github.com/McPants/jquery.shapeshift/issues/48 https://github.com/McPants/jquery.shapeshift/issues/48
在每次拖放时创建/更新JavaScript对象,并将ajax请求发送到控制器,您可以在其中读取对象并将其存储在数据库中。
First, modify your Javascript to create an array of ordered pairs and send it to your endpoint: 首先,修改Javascript以创建有序对数组并将其发送到端点:
jQuery(function() {
$('.listwrap').shapeshift();
$('.listwrap').on('ss-rearranged', function(e) {
ordered_items = [];
$(this).children().each(function() {
ordered_items.push([$(this).attr("id"), $(this).index()]);
});
$.post('/boards/sortlists',
{ordered_items: JSON.stringify(ordered_items)},
function(data, status, jqXHR) {
// This is what gets rendered from your rails controller + action
});
});
});
Then, review what it looks like on the Rails side and do something with it 然后,查看Rails方面的外观并对其进行处理
class BoardsController < ApplicationController
# ...
def sortlists
logger.info params[:ordered_items]
end
# ...
end
The output of that will go to log/development.log 其输出将转到log / develop.log
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.