Rails3: ajax responds with js template text!

hey guys,

I am having an issue with AJAX on rails 3. I don’t know if it’s a bug or
something but definitely weird. Basically i have {model,controller,view}
that work perfectly over http.

I have an index action in my “articles” controller that searches for a
search query supplied from the form on the index view.

the weird behavior:

  1. using firebug, i can see that the AJAX request is sent to rails and
    the response i get is the content of the file index.js.erb! and
    obviously nothing changes on the page…

rails console log shows that the template index.js.erb was rendered
successfully and all is OK (200).

my application layout has:

<head>
  <title>Blog</title>
  <%= stylesheet_link_tag :all %>
  <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tag %>
</head>

i.e. all prototype is loaded

my index action:

 def index

    if params[:search]
    @articles = Article.search params[:search]
    else
    @articles = Article.all
    end
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @articles }
      format.js
    end
  end

my index.erb.html

<%= form_tag articles_path, :method => :get, :remote => true do %>
<%= text_field_tag :search %>
<%= submit_tag :search %>
<% end %>

<div id="listing">
<%= render :partial => 'list' , :locals => { :articles => @articles } %>
</div>
<%= link_to 'New Article', new_article_path %>

my index.js.erb has:

page.replace_html 'listing', partial => 'list', :locals => { :articles
=> @articles }

the content of the js.erb template is what is returned in the response!
why is this happening???

  1. the second weird behavior:
    if I change the JS template file from .js.erb to .rjs, rails renders the
    http template file!!! even though it received and AJAX request (as
    evident in firebug), that’s really weird…

Please shed some light on why all of this is happening and how do i get
it my partial to replace the ‘listing’ div html.

On Sep 30, 10:18 am, Naif D. [email protected] wrote:

my index.js.erb has:

page.replace_html 'listing', partial => 'list', :locals => { :articles
> => @articles }

the content of the js.erb template is what is returned in the response!
why is this happening???

Because the above is not an erb template - or rather it contains no
erb tags (<%= and so on).
I believe the ‘correct’ name for rjs templates these days is js.rjs.

Fred

Frederick C. wrote:

On Sep 30, 10:18�am, Naif D. [email protected] wrote:

my index.js.erb has:

page.replace_html 'listing', partial => 'list', :locals => { :articles
>> => @articles }

the content of the js.erb template is what is returned in the response!
why is this happening???

Because the above is not an erb template - or rather it contains no
erb tags (<%= and so on).
I believe the ‘correct’ name for rjs templates these days is js.rjs.

Fred

Thank you very, very much Fred. You saved my day man!

I am so angry that the extension change was never mentioned in rails 3
documentation site… how are people supposed to know about that if the
development team doesn’t announce that?!

Thank you again Fred, very much appreciated.

On Sep 30, 11:26 am, Naif D. [email protected] wrote:

Frederick C. wrote:

I am so angry that the extension change was never mentioned in rails 3
documentation site… how are people supposed to know about that if the
development team doesn’t announce that?!

I thought the ‘new’ extensions were added in 2.0 and the old ones had
been vaguely deprecated ever since.

Fred

Naif D. wrote:

I am so angry that the extension change was never mentioned in rails 3
documentation site… how are people supposed to know about that if the
development team doesn’t announce that?!

This isn’t a new change at all, and especially not specific to Rails 3.
The code you had inside of your js.erb template was RJS code, which will
really limit you in places. The appropriate javascript equivalent inside
of your js.erb template would be:

$(’#listing’)
.replace("<%= escape_javascript(render :partial => ‘list’, :locals =>
{ :articles => @articles }) %>");

Note that is jQuery and not Prototype, but it applies regardless.
Embrace the Javascript!

Parker S. wrote:

Naif D. wrote:

I am so angry that the extension change was never mentioned in rails 3
documentation site… how are people supposed to know about that if the
development team doesn’t announce that?!

This isn’t a new change at all, and especially not specific to Rails 3.
The code you had inside of your js.erb template was RJS code, which will
really limit you in places. The appropriate javascript equivalent inside
of your js.erb template would be:

$(’#listing’)
.replace("<%= escape_javascript(render :partial => ‘list’, :locals =>
{ :articles => @articles }) %>");

Note that is jQuery and not Prototype, but it applies regardless.
Embrace the Javascript!

Thank you Fred & Parker… You guys are awesome!.. I guess I haven’t
kept track of rails over the past years…

Parker S. wrote:

Naif D. wrote:

I am so angry that the extension change was never mentioned in rails 3
documentation site… how are people supposed to know about that if the
development team doesn’t announce that?!

This isn’t a new change at all, and especially not specific to Rails 3.
The code you had inside of your js.erb template was RJS code, which will
really limit you in places. The appropriate javascript equivalent inside
of your js.erb template would be:

$(’#listing’)
.replace("<%= escape_javascript(render :partial => ‘list’, :locals =>
{ :articles => @articles }) %>");

Note that is jQuery and not Prototype, but it applies regardless.
Embrace the Javascript!

Better yet, use static JavaScript files and banish js.erb from your
vocabulary. (RJS may occasionally be useful, but in general JS
shouldn’t be dynamically generated IMHO.)

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs