0

我是 Jquery 的新手。将参数传递给 Jquery 函数时出现问题。我的应用程序有嵌套资源。

class Post < ActiveRecord::Base
  has_many :votes

路线是

sort_votes_post POST   /posts/:id/sort_votes(.:format)    posts#sort_votes

可排序功能是

$(function() {  
$("#sortable").sortable({
    update: function() {
        $.ajax({
            type: 'post',
            data: $('#sortable').sortable('serialize'),
            url: '/posts/[not sure how to pass :id here]/sort_votes'})
    }
});
$("#sortable").disableSelection();
});

不知道如何将 :id 传递给 url 选项。

4

1 回答 1

0

如果您的代码在资产管道中,我不确定您是否可以,因为管道是预编译的,因此任何动态细节都不起作用。

这有点 hacky 但我建议从视图中传递一个变量,所以在添加 application.js 之前添加

<script>
   var url = [your url]
</script>

然后在js中

$(function() {  
$("#sortable").sortable({
    update: function() {
        $.ajax({
            type: 'post',
            data: $('#sortable').sortable('serialize'),
            url: url
    }
});
$("#sortable").disableSelection();
});
于 2012-08-19T07:58:20.273 回答