ruby on rails - Rails3 + jQuery: Ajax response messed up -


i'm trying along rails 3 , ajaxification of app jquery. therefore, when user writing comment, comment should inserted via ajax after hitting submit button.

problem is: ajax response contains whole generated html of application.haml. i'm having no clue, how rid of html , intended response written in related create.js.erb file.

here's comments controller:

class commentscontroller < applicationcontroller    def new     @comment = comment.new   end    def create     @article = article.find(params[:article_id])     @comment = current_user.comments.build(params[:comment])     @comment.article_id = @article.id     respond_to |format|       if @comment.save         format.html         format.js        else         format.html         format.js       end     end   end  end 

the form (in haml) comments looking this:

  #article_write_comment     %h2 write comment     #article_write_comment_form       %p comments limited 1000 chars.       = form_for([@article, @article.comments.build], :remote => true) |f|         .field           = f.text_area :body, :rows => 10, :cols => 50         .actions           = f.submit "submit" 

my create.js.erb file placed in views/comments:

$('#article_comments').append('<%= escape_javascript(render(@comment)) %>'); 

the comment partial (views/comments/_comment.haml), should appended looks this:

.comment   = gravatar_for comment.user, :size => 48   %p     %b       = comment.user.name       said:      = time_ago_in_words(comment.created_at)     ago   %p     = simple_format(comment.body) 

finally, sent ajax post after hitting submit is:

authenticity_token  e2rvckyjpex8f31u2vemxcgmfvjzxvqto+03ocasnkw= comment[body]   test test test commit  submit utf8    ✓ 

but instead of kind of response:

$('#article_comments').append('<%= escape_javascript(render(@comment)) %>'); 

i'm getting kind of response html code of site: (shortened content of divs less distraction)

<!doctype html> <html>   <head>   </head>   <body>     <div id='content'>       <div id='header'>       </div>       <div id='container'>         $('#article_comments').append('<div class=\'comment\'>\n  <img alt=\"alex gieche\" class=\"gravatar\" src=\"http://gravatar.com/avatar/66d7f7aefd1f45ea810cb3f524cc1780?size=48\" />\n  <p>\n    <b>\n      alex gieche\n      said:\n    <\/b>\n    less minute\n    ago\n  <\/p>\n  <p>\n    <p>test test test<\/p>\n  <\/p>\n<\/div>\n');       </div>       <div id='sidebar'>       </div>       <div id='footer'>       </div>     </div>   </body> </html> 

the comment not getting appended div (but written database). so, how can fix this? looking!

ok, looking, found solution. in comments controller,i had add following hash format.js prevent rendering layout in response:

class commentscontroller < applicationcontroller    def new     @comment = comment.new   end    def create     @article = article.find(params[:article_id])     @comment = current_user.comments.build(params[:comment])     @comment.article_id = @article.id     respond_to |format|       if @comment.save         format.html         format.js {render :layout=>false}       else         format.html         format.js {render :layout=>false}       end     end   end  end 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -