The problem with Ajax based micropost's comments

I have ajax based micropost’s comments on the user page. It works, but
not correctly. When I submit a new comment to ANY micropost, it always
posts to the LAST micropost! Then, if I refresh the page by “F5”,
everything falls into place - the new comment is in the correct
micropost. What I’m doing wrong? Thanks in advance.

comment.rb

class Comment < ActiveRecord::Base
attr_accessible :comment_content
belongs_to :user
belongs_to :micropost
end

comments_controller.rb

class CommentsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]

def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(params[:comment])
@comment.micropost = @micropost
@comment.user = current_user
respond_to do |format|
@comment.save
format.html { redirect_to current_user }
format.js
end
end
end

_micropost.html.erb

<%= wrap(micropost.content) %> Posted <%= time_ago_in_words(micropost.created_at) %> ago. <%= render 'shared/comment_form', micropost: micropost %>
<%= render micropost.comments %>

_comment_form.html.erb

<%= form_for ([micropost, @comment]), :remote => true do |f| %>
<%= render ‘shared/error_messages’, object: f.object %>
<%= f.text_area :comment_content, :size => “40x2” %>

Comment

<% end %>

_comment.html.erb

<%=
wrap(comment.comment_content) %>


Posted by <%= comment.user.name %> <%=
time_ago_in_words(comment.created_at) %> ago.

create.js.erb

$(’#comments’).html("<%= escape_javascript(render(:partial =>
@micropost.comments)) %>");

On 15 March 2012 16:27, Groovor C. [email protected] wrote:

I have ajax based micropost’s comments on the user page. It works, but
not correctly. When I submit a new comment to ANY micropost, it always
posts to the LAST micropost! Then, if I refresh the page by “F5”,
everything falls into place - the new comment is in the correct
micropost. What I’m doing wrong? Thanks in advance.

First look in development.log and check that the correct parameters
are being passed in. Then have a look at the Rails Guide on Debugging
which will show you a number of techniques that can be used to debug
the code.

Colin

comments_controller.rb
@comment.save

To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


gplus.to/clanlaw