我需要通过JavaScript将参数传递回服务器。目前,我是这样将它们传递到JavaScript中的:
sendParams("<%= params[:q].to_json %>");
然后像这样将它们发送回去:
function sendParams(q){
$.ajax({
url: '/mymodel/myaction',
type: 'post',
data: {'q':q},
contentType: 'json'
});
}
在我的控制器中,我试图像处理其他参数一样使用它们:
MyModel.where(params[:q])
但是参数为空,尽管Firebug在“POST”选项卡中显示如下内容:
q=%7B%26quot%3Bc%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Ba%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bname%26quot%3B%3A%26quot%3Btitle%26quot%3B%7D%7D%2C%26quot%3Bp%26quot%3B%3A%26quot%3Bcont%26quot%3B%2C%26quot%3Bv%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bvalue%26quot%3B%3A%26quot%3B2%26quot%3B%7D%7D%7D%7D%2C%26quot%3Bs%26quot%3B%3A%7B%26quot%3B0%26quot%3B%3A%7B%26quot%3Bname%26quot%3B%3A%26quot%3Bvotes_popularity%26quot%3B%2C%26quot%3Bdir%26quot%3B%3A%26quot%3Bdesc%26quot%3B%7D%7D%7D
你知道为什么这些信息没有被where子句处理吗?我应该怎么做才能让params再次变得Rails可读?
更新:
Started POST "/publications/search?scroll=active&page=6" for 127.0.0.1 at 2013-0
2-12 22:55:24 -0600
Processing by PublicationsController#index as */*
Parameters: {"scroll"=>"active", "page"=>"6"}
更新2:
问题显然源于contentType
。当我将其移除后,q
作为Rails参数发送。不幸的是,q
仍然是JSON格式,导致出现错误:
undefined method `with_indifferent_access' for #<String:0x686d0a8>
我该如何将JSON转换为参数哈希表?