Rails将ActiveRecord :: Relation作为json返回到ajax调用

编程入门 行业动态 更新时间:2024-10-23 21:32:06
本文介绍了Rails将ActiveRecord :: Relation作为json返回到ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好我有一个ajax电话:

Hi I've got a ajax call:

$.ajax({ url: '<%= url_for( :controller => :users , :action => :search_user) %>', data: {search: $("#suchfeld").val()} }) .done(function (data) { alert(msg); })

现在我希望来自控制器的返回值,活动记录对象将作为json对象返回。

and now I want that the return value from controller the active record object will return as a json object.

我试过这个:

@users_search = User.where("lastname LIKE ?", "#{params[:search]}%").to_json

但我没有得到我想要回来的东西。

But I didn't got back what I want to get back.

@users_search是一个对象,所有用户都对应上述(工作)查询。

the @users_search is an object with all users corresponding the above (working) query.

我得到了som HTML代码返回不是作为json对象的Active记录对象

I got som HTML code back Not the Active record object as a json object

我想要一个json对象,因为我想迭代它

I want an json object, because I want to iterate over it

也试过这个:

.done(function () { var array = <%= @users_search.to_json %>; alert(array); })

数组始终为空

编辑:

将代码更改为:

@userss = User.where("lastname LIKE ?", "#{params[:search]}%") for user in @userss do @users_to_add << {:id => user.id, :lastname => user.lastname} end @users_search << {:results => @users_to_add}.to_json

数组在控制台中看起来像:

array looks in console like:

["{\"results\":[{\"id\":1,\"lastname\":\"Hohlwegler\"},{\"id\":2,\"lastname\":\"Hohlwegler\"},{\"id\":5,\"lastname\":\"Hohlwegler\"},{\"id\":6,\"lastname\":\"Hohlwegler\"},{\"id\":11,\"lastname\":\"Hohlwegler\"}]}"]

更新:

必须将其更改为:

respond_to do |format| format.json { render :json => { :results => @userss.map{ |u| { :id => u.id, :lastname => u.lastname } } } } end

返回:[Object object]

This returns: [Object object]

.done(function (arr) { alert(arr); })

如果我发出警报(arr [0] .data);我得不到未定义的数据

if I do alert(arr[0].data); I got cannot read data of undefined

推荐答案

我认为你混合了两个不同的概念。

I think you are mixing two different concepts.

Rails可以处理不同类型/格式的请求,例如html(默认值),json,js,xml等。

Rails can handle different types/formats of requests, for example html (the default), json, js, xml, etc.

它不是在您的代码中可以看到控制器如何处理请求所以我将其作为html处理。

It is not visible in your code how the controller is handling the request so I guest it is handing as html.

另一方面,jquery不知道您正在使用的技术在服务器中,因此尝试打印控制器变量不起作用。

In the other hand, jquery is not aware about the technology you are using in the server so trying to print the controller variable are not going to work.

进行以下更改以尝试解决问题:

Do the following changes to try to fix the issue:

in users#search_user

users = User.where("lastname LIKE ?", "#{params[:search]}%") respond_to do |format| format.json { render json: { results: users.map{ |u| { id: u.id, lastname: u.name } } } } end

在您看来:

$.ajax({ url: '<%= url_for( :controller => :users , :action => :search_user) %>', data: {search: $("#suchfeld").val()}, dataType: 'json' }).done(function (data) { alert(data); })

更新 您可以使用 JSON.stringify 查看'data'对象:

UPDATE You can see the 'data' object using JSON.stringify:

alert(JSON.stringify(data));

此外,控制器还返回一个具有以下格式的对象:

Also the controller is returning an object with the following format:

{ results: [{id: 1, lastname: 'foo'}, {id: 2, lastname: 'bar'}] }

因此,如果你想获得第一个姓氏,你应该使用类似的东西: data.results [0] .lastname

So if you want to get the first lastname you should use something like: data.results[0].lastname

更多推荐

Rails将ActiveRecord :: Relation作为json返回到ajax调用

本文发布于:2023-10-17 10:33:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500661.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ActiveRecord   Rails   Relation   ajax   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!