RoR、Ajax、可排序、序列化
我只是有一点“呃”时刻,但我有一个菜单项列表,我可以对其进行排序,并使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是你的问题,正如我怀疑的那样:
serialize
返回这样的字符串:menu[]=2&menu[]=3&menu[]=1&menu[]=4< /代码>。然后,您将该字符串分配给新对象中名为
sort
的属性,并将该对象作为data
参数。结果,实际以POST
数据发送到服务器的内容如下所示:..Rails(正确地)将其解释为带有解码值的单个参数
sort
menu[]=2&menu[]=3&menu[]=1&menu[]=4
。明白我要说的是什么吗?您需要将该序列化字符串作为
POST
直接发送到服务器,而不是采用该序列化字符串并将其设置为另一个参数的值> 数据。解决这个问题很简单。将您的$.ajax
调用更改为:这将按照您想要的方式发送到服务器:
..Rails 将收到您期望的内容:
Here's your problem, just as I suspected:
serialize
returns a string like this:menu[]=2&menu[]=3&menu[]=1&menu[]=4
. Then you're assigning that string to a property calledsort
in a new object, and giving that object as thedata
parameter. As a result what actually gets sent to the server asPOST
data that looks something like this:..which Rails (correctly) interprets as a single parameter
sort
with the decoded valuemenu[]=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:This will be sent to the server like you want:
..and Rails will receive what you expect: