Topic: Error during failsafe response: Rails 3

I have a form_tag that works fine using html, but when I use ajax with the remote => true I am getting this error:-
My terminal log shows:-
Started GET "/" for at 2010-11-01 01:19:49 +0000
  Processing by HomepagesController#index as HTML
  Homepage Load (0.6ms)  SELECT "homepages".* FROM "homepages"
Rendered homepages/index.html.erb within layouts/application (23.0ms)
Completed 200 OK in 40ms (Views: 27.3ms | ActiveRecord: 0.6ms)
Error during failsafe response: incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string)
***** then a load of cleaner.rb stuff
Started GET "/homepages?utf8=%E2%9C%93&search=hom" for at 2010-11-01 01:19:56 +0000
  Processing by HomepagesController#index as JS
  Parameters: {"utf8"=>"✓", "search"=>"hom"}
  Homepage Load (0.5ms)  SELECT "homepages".* FROM "homepages" WHERE (section LIKE '%hom%')
Rendered homepages/index.js.erb (2.9ms)
Completed   in 19ms

In my index.js.erb I have:-
$("testsearch").update("<%= escape_javascript(render(@homepages))%>");

and in my Controller I have:-
  def index   
    @homepages =[:search])
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @homepages }
      format.js { render :layout => false }
in my view I have:-
<div id = "testsearch"> which prints @homepages using a table using <% @homepages.each do |homepage| %> which is not being updated.

Anyone have any ideas as to why I get this error.

Re: Error during failsafe response: 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

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: Error during failsafe response: Rails 3

I have cracked it by going onto an IRC chat room ( 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.