Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery + Ajax + Haml. js.erb files not firing

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?

like image 819
kelly.dunn Avatar asked Dec 17 '22 22:12

kelly.dunn


1 Answers

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
like image 62
kelly.dunn Avatar answered Feb 15 '23 17:02

kelly.dunn