RoR、Ajax、可排序、序列化

发布于 2024-12-09 19:33:55 字数 1860 浏览 1 评论 0原文

我只是有一点“呃”时刻,但我有一个菜单项列表,我可以对其进行排序,并使用 jquery ui 的序列化方法序列化数据。我正在提交 ajax 请求,并在 webrick 中看到以下参数:

参数: { "sort" =>; "menu[]=2&menu[]=3&menu[]=1&menu[]=4" }

最后,我想要一个 2,3,1 的列表,4 并编写一个 SQL 查询来更新这些项目的 sort_order。到目前为止,我发现的大多数教程都只有 PHP 服务器端示例,人们只是使用 params['sort']['menu'] 返回 2,3, 1,4 但在 Ruby 中似乎返回一个空对象。

这是我的 jquery 代码:

$(document).ready(function() {
  $(".sortable").sortable({
    update : function (){ 
      $.ajax({
        type  : "POST",
        url   : "/page_parts/sort/",
        data  : {
          sort : $(".sortable").sortable('serialize')
        }
      }); 
    }   
  }); 
});

这是我的标记:

<ul class="sortable">
  <% @page_parts.each do |f| %>
    <li id="menu_<%= f.id %>"><%= f.title %></li>
  <% end %>
</ul>

这是我的控制器代码:

def sort
  raise params['sort']['menu'].inspect
end 

将返回:

Started POST "/page_parts/sort/" for 127.0.0.1 at 2011-10-12 06:19:38 -0400
  Processing by PagePartsController#sort as */*
  Parameters: {"sort"=>"menu[]=1&menu[]=2&menu[]=4&menu[]=3&menu[]=5"}
Completed 500 Internal Server Error in 0ms

RuntimeError ("menu"):
  app/controllers/page_parts_controller.rb:9:in `sort'

如果另一方面我尝试:

def sort
  raise params['sort'].inspect
end 

我得到:

Started POST "/page_parts/sort/" for 127.0.0.1 at 2011-10-12 06:19:10 -0400
  Processing by PagePartsController#sort as */*
  Parameters: {"sort"=>"menu[]=1&menu[]=2&menu[]=3&menu[]=5&menu[]=4"}
Completed 500 Internal Server Error in 0ms

RuntimeError ("menu[]=1&menu[]=2&menu[]=3&menu[]=5&menu[]=4"):

I'm having just a bit of a 'duh' moment, but i have a list of menu items that i made sortable and have serialized the data using jquery ui's serialize method. I'm submitting the ajax request and and seeing the following parameters in webrick:

Parameters: { "sort" => "menu[]=2&menu[]=3&menu[]=1&menu[]=4" }

Ultimately, I'd just like to have a list of 2,3,1,4 and write a sql query to update the sort_order for those items. Most of the tutorials I've found thus far have had only PHP server-side examples and people are just going params['sort']['menu'] which returns 2,3,1,4 but in Ruby appears to return an empty object.

This is my jquery code:

$(document).ready(function() {
  $(".sortable").sortable({
    update : function (){ 
      $.ajax({
        type  : "POST",
        url   : "/page_parts/sort/",
        data  : {
          sort : $(".sortable").sortable('serialize')
        }
      }); 
    }   
  }); 
});

This is my markup:

<ul class="sortable">
  <% @page_parts.each do |f| %>
    <li id="menu_<%= f.id %>"><%= f.title %></li>
  <% end %>
</ul>

This is my controller code:

def sort
  raise params['sort']['menu'].inspect
end 

That will return:

Started POST "/page_parts/sort/" for 127.0.0.1 at 2011-10-12 06:19:38 -0400
  Processing by PagePartsController#sort as */*
  Parameters: {"sort"=>"menu[]=1&menu[]=2&menu[]=4&menu[]=3&menu[]=5"}
Completed 500 Internal Server Error in 0ms

RuntimeError ("menu"):
  app/controllers/page_parts_controller.rb:9:in `sort'

If on the other hand i try:

def sort
  raise params['sort'].inspect
end 

I get:

Started POST "/page_parts/sort/" for 127.0.0.1 at 2011-10-12 06:19:10 -0400
  Processing by PagePartsController#sort as */*
  Parameters: {"sort"=>"menu[]=1&menu[]=2&menu[]=3&menu[]=5&menu[]=4"}
Completed 500 Internal Server Error in 0ms

RuntimeError ("menu[]=1&menu[]=2&menu[]=3&menu[]=5&menu[]=4"):

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流星番茄 2024-12-16 19:33:55

这是你的问题,正如我怀疑的那样:

data  : {
  sort : $(".sortable").sortable('serialize'),
},

serialize 返回这样的字符串: menu[]=2&menu[]=3&menu[]=1&menu[]=4< /代码>。然后,您将该字符串分配给新对象中名为 sort 的属性,并将该对象作为 data 参数。结果,实际以 POST 数据发送到服务器的内容如下所示:

POST /page_parts/sort/ HTTP/1.1
Content-Type: application/x-www-form-urlencoded

sort=menu%5B%5D%3D2%26menu%5B%5D%3D3%26menu%5B%5D%3D1%26menu%5B%5D%3D4

..Rails(正确地)将其解释为带有解码值的单个参数 sort menu[]=2&menu[]=3&menu[]=1&menu[]=4

明白我要说的是什么吗?您需要将该序列化字符串作为 POST 直接发送到服务器,而不是采用该序列化字符串并将其设置为另一个参数的值> 数据。解决这个问题很简单。将您的 $.ajax 调用更改为:

$.ajax(
  { type  : 'POST',
    url   : '/page_parts/sort/',
    data  : $('.sortable').sortable('serialize') // on its own, no object
  } 
);

这将按照您想要的方式发送到服务器:

POST /page_parts/sort/ HTTP/1.1
Content-Type: application/x-www-form-urlencoded

menu[]=2&menu[]=3&menu[]=1&menu[]=4

..Rails 将收到您期望的内容:

>> params
# => { :menu => [ 2, 3, 1, 4 ] }
>> params[:menu]
# => [ 2, 3, 1, 4 ]

Here's your problem, just as I suspected:

data  : {
  sort : $(".sortable").sortable('serialize'),
},

serialize returns a string like this: menu[]=2&menu[]=3&menu[]=1&menu[]=4. Then you're assigning that string to a property called sort in a new object, and giving that object as the data parameter. As a result what actually gets sent to the server as POST data that looks something like this:

POST /page_parts/sort/ HTTP/1.1
Content-Type: application/x-www-form-urlencoded

sort=menu%5B%5D%3D2%26menu%5B%5D%3D3%26menu%5B%5D%3D1%26menu%5B%5D%3D4

..which Rails (correctly) interprets as a single parameter sort with the decoded value menu[]=2&menu[]=3&menu[]=1&menu[]=4.

See where I'm going with this? Instead of taking that serialized string and setting it as the value of another parameter, you need to send that serialized string directly to the server as the POST data. The fix for this is easy. Change your $.ajax call to:

$.ajax(
  { type  : 'POST',
    url   : '/page_parts/sort/',
    data  : $('.sortable').sortable('serialize') // on its own, no object
  } 
);

This will be sent to the server like you want:

POST /page_parts/sort/ HTTP/1.1
Content-Type: application/x-www-form-urlencoded

menu[]=2&menu[]=3&menu[]=1&menu[]=4

..and Rails will receive what you expect:

>> params
# => { :menu => [ 2, 3, 1, 4 ] }
>> params[:menu]
# => [ 2, 3, 1, 4 ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文