MDM
MDM

Reputation: 77

Ajax search, Javascript, Rails 3

After doing a search of @homepages using ajax I want to update my div with:- $("testsearch").update("<%= escape_javascript(render(@homepages)%>"); in my index.js.erb

Which does not work as I am getting an internal server error:- Error during failsafe response: incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string)

Anyone has any ideas why I am getting this error.

As a test the following renders OK. $("testsearch").update("<%= escape_javascript(render :text =>'this is UJS')%>");

Upvotes: 0

Views: 614

Answers (1)

MDM
MDM

Reputation: 77

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.

Apparently what was happening was that I was following Ryan Bates who does many extremely good Railcast videos, but he builds on previous Railcast. Therefore in his 205 Railscast, which deals with Ajax calls, he did not mention that you must have:-

format.js in the action in the controller.

His xxxx.searchxxxxx needs to be created in the controller or model.

And that when I did :-

<%= render(@homepages)%> (in his case <%= 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).

And then in "_homepage" I would add my code to render the results.

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". Now I can use "_homepage" for the html and the Ajax call.

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. At the moment I do not know why, but at least the Ajax call is working.

Upvotes: 1

Related Questions