简体   繁体   English

在ajax调用之后,@ variable以错误的格式返回

[英]@variable returns in the wrong format after an ajax call

Does anyone knows how I can influence the format of the instance variable when it is returned after an ajax call. 有人知道在ajax调用后返回实例变量时,如何影响实例变量的格式。

At the moment when I try to render it I get the error:- incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string) 当我尝试渲染它时,出现错误:-不兼容的编码正则表达式匹配(带有ASCII-8BIT字符串的UTF-8正则表达式)

This happens when the instance variable is being rendered in the index.js.erb file. 当实例变量在index.js.erb文件中呈现时,会发生这种情况。

It seems that the format.js is returning the @variable in the wrong format. 看来format.js以错误的格式返回了@variable。

I have cracked it by going onto an IRC chat room (irc.freenode.net RubyonRails) and a ProjectZen (human being somewhere out there in the ether) helped me to get it working. 我通过进入IRC聊天室(irc.freenode.net RubyonRails)和一个ProjectZen(人类在以太坊中的某个地方)来破解它,帮助我使其正常工作。

Apparently what was happening was that I was following Ryan Bates who does many extremely good Railcast videos, but he builds on previous Railcast. 显然发生了什么事,我正在关注Ryan Bates,他制作了许多非常出色的Railcast视频,但是他是在以前的Railcast的基础上开发的。 Therefore in his 205 Railscast, which deals with Ajax calls, he did not mention that you must have:- 因此,在处理Ajax调用的205 Railscast中,他没有提到您必须具备:-

format.js in the action in the controller. 控制器中动作中的format.js。

His xxxx.searchxxxxx needs to be created in the controller or model. 他的xxxx.searchxxxxx需要在控制器或模型中创建。

And that when I did :- 而且当我这样做时:-

<%= render(@homepages)%> (in his case <%= render(@products)%>) <%= render(@homepages)%>(在他的情况下,<%= render(@products)%>)

The render was looking for a partial called "_homepage" (not "homepages") (I did not even have a partial therefore I got the UTF8 to ASCII error). 渲染器正在寻找一个名为“ _homepage”的部分(而不是“ homepages”)(我什至没有一个部分,因此我收到了UTF8到ASCII错误)。

And then in "_homepage" I would add my code to render the results. 然后在“ _homepage”中添加我的代码以呈现结果。

What I have now done in my index.html.erb is to put <%= render(@homepages)%> , in the (div id = testsearch) in place of the code I use to render @homepages and then place that code in a partial "_homepage". 我现在在index.html.erb中所做的是将<%= render(@homepages)%>放在(div id = testsearch)中,以代替用于渲染@homepages的代码,然后将该代码放置在部分“ _homepage”中。 Now I can use "_homepage" for the html and the Ajax call. 现在,我可以将“ _homepage”用于html和Ajax调用。

At the moment I have a slight problem in that it is rendering all the data in the"@homepages" as many times as the number of records. 目前,我有一个小问题,那就是它渲染“ @homepages”中的所有数据的次数是记录数量的多少倍。 At the moment I do not know why, but at least the Ajax call is working. 目前,我不知道为什么,但是至少Ajax调用正在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM