Note that there are some explanatory texts on larger screens.

plurals
  1. PORails3 + jQuery: Ajax response messed up
    primarykey
    data
    text
    <p>I'm trying to get along with Rails 3 and some Ajaxification of my app with jQuery. Therefore, when a user is writing a comment, the comment should be inserted via Ajax after hitting the submit button.</p> <p>Problem is: The Ajax response contains the whole generated html of my application.haml. I'm having no clue, how to get rid of this html and only get the intended response written in the related create.js.erb file.</p> <p>Here's my comments controller:</p> <pre><code>class CommentsController &lt; 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 do |format| if @comment.save format.html format.js else format.html format.js end end end end </code></pre> <p>The form (in HAML) for comments is looking like this:</p> <pre><code> #article_write_comment %h2 Write a comment #article_write_comment_form %p Comments are limited to 1000 chars. = form_for([@article, @article.comments.build], :remote =&gt; true) do |f| .field = f.text_area :body, :rows =&gt; 10, :cols =&gt; 50 .actions = f.submit "Submit" </code></pre> <p>My create.js.erb file is placed in views/comments:</p> <pre><code>$('#article_comments').append('&lt;%= escape_javascript(render(@comment)) %&gt;'); </code></pre> <p>The comment partial (views/comments/_comment.haml), that should be appended looks like this:</p> <pre><code>.comment = gravatar_for comment.user, :size =&gt; 48 %p %b = comment.user.name said: = time_ago_in_words(comment.created_at) ago %p = simple_format(comment.body) </code></pre> <p>Finally, the sent ajax post after hitting submit is:</p> <pre><code>authenticity_token e2rvcKyjPEx8f31U2vemxCGMfVJzxvqTO+03OCAsNkw= comment[body] Test Test Test commit Submit utf8 ✓ </code></pre> <p>But instead of this kind of response:</p> <pre><code>$('#article_comments').append('&lt;%= escape_javascript(render(@comment)) %&gt;'); </code></pre> <p>I'm getting this kind of response with the html code of the site: (Shortened the content of the divs for less distraction)</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;div id='content'&gt; &lt;div id='header'&gt; &lt;/div&gt; &lt;div id='container'&gt; $('#article_comments').append('&lt;div class=\'comment\'&gt;\n &lt;img alt=\"Alex Gieche\" class=\"gravatar\" src=\"http://gravatar.com/avatar/66d7f7aefd1f45ea810cb3f524cc1780?size=48\" /&gt;\n &lt;p&gt;\n &lt;b&gt;\n Alex Gieche\n said:\n &lt;\/b&gt;\n less than a minute\n ago\n &lt;\/p&gt;\n &lt;p&gt;\n &lt;p&gt;Test Test Test&lt;\/p&gt;\n &lt;\/p&gt;\n&lt;\/div&gt;\n'); &lt;/div&gt; &lt;div id='sidebar'&gt; &lt;/div&gt; &lt;div id='footer'&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The comment is not getting appended to the div (but is written to the database). So, how can I fix this? Thanks for looking!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload