Topic: 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')%>");

Re: Ajax search, Javascript, Rails 3

I just found 2 things that might have a bearing on this.
In Javascript
1)escape() This function encodes special characters, with the exception of: * @ - _ + . /

In Rails 3
2)escape_javascript(javascript)

Escape carrier returns and single and double quotes for JavaScript segments.

[ show source ]

      # File actionpack/lib/action_view/helpers/javascript_helper.rb, line 50
50:       def escape_javascript(javascript)
51:         if javascript
52:           javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] }
53:         else
54:           ''
55:         end
56:       end

It looks like the escape_javascript function does not like @
I am not sure what the Javascript.gosub does as yet.
Anyone has any ideas.

Re: Ajax search, Javascript, Rails 3

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