4

After reading up on a few experiences, I feel this issue might need to be brought up again. Coding in Rails3, I'm trying to implement some smooth Ajax effects when a user tries to create a post on another element of my app. Here's the code I'm concerned with:

app/views/posts/new.html.haml

- form_for @post, :remote=>true do |f|
    = f.text_area :content
    = submit_tag "Post"

app/controllers/post_controller.rb

def create
  @comment = Post.new(params[:post])
  @comment.save
end

app/views/posts/create.js.erb:

alert("ajax worked!");

I've followed what I saw on the UJS Railscast, but nothing seems to be firing. Not only that, but firebug fails to give me any descriptive evidence of what's happening. It tells me that it made the new post object upon submission, but nothing further.

Any ideas?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
kelly.dunn
  • 1,546
  • 3
  • 16
  • 23

3 Answers3

12

I found the answer! The issue was that when the controller was rendering the view, it was including the overall layout of my application; the layout was yielding to the rendering action, so my javascript code contained inside of my .js.erb file was spit out into my application.rhtml. I fixed this issue by including this inside of my controller action to display my posts:

respond_to do |format|
  format.js {render :layout=>false}
end
kelly.dunn
  • 1,546
  • 3
  • 16
  • 23
  • I get the same problem (http://stackoverflow.com/q/2738361/328998), and I consider the `render :layout=>false` trick as a temporary solution… It should not render the .js file embeded in layout, that does not make sense… – Yannis Oct 01 '10 at 11:49
  • It didn't make sense to me either. I tried the approach you listed (with telling rails to render the MIME type) but it did not work for me; it still wanted to spit out my js.erb content into the layout of the specified controller. – kelly.dunn Oct 01 '10 at 17:17
  • Reading this answer was the culmination of hours of frustration. It wasn't the answer to my question, but it got me to look in the right place at just the right time. Thank you so so much. – Ziggy Apr 03 '13 at 05:07
0

Watch a Railscast

<%= form_tag products_path, :method => 'get', :id => ↵  
  "products_search" do %>  
  <%= hidden_field_tag :direction, params[:direction] %>  
  <%= hidden_field_tag :sort, params[:sort] %>  
  <p>  
    <%= text_field_tag :search, params[:search] %>  
    <%= submit_tag "Search", :name => nil %>  
  </p>  
<% end %>

and in Js

// Search form.  
  $('#products_search').submit(function () {  
    $.get(this.action, $(this).serialize(), null, 'script');  
    return false;  
  });  
});
Alexander Paramonov
  • 1,441
  • 14
  • 24
0

When my layout was coded in Haml (application.haml), AJAX wouldn't fire and the work-around code kelly.dunn mentioned didn't work.

respond_to do |format|
  format.js {render :layout=>false}
end

The easiest fix was to convert the application layout to .html.erb format.

nslocum
  • 5,037
  • 1
  • 27
  • 27