Add js respond format to comments controller
def index @comments = Comment.all end def create @comment = Comment.new(params[:comment]) @comment.save respond_with @comment, :location => comments_url end end
app/views/comments/index.html.erb with remote form
<div id="comments_count"><%= comments_count %></div> <div id="comments"> <%= render @comments %> </div> <h3>Add your comment:</h3> <%= form_for Comment.new, :remote => true do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <p> <%= f.label :content, "Comment" %><br /> <%= f.text_area :content, :rows => '12', :cols => 35 %> </p> <p><%= f.submit %></p> <% end %>
app/views/comments/create.js.coffee) to handle ajax request
$('#new_comment').reset() $('#comments_count').html '<%= comments_count %>'
The complete source is on Github. So feel free to checkout and run it to see in action.