使用AJAX更新

I have this chat box

    <h1>Chat</h1>

    <div>
      <% @messages.each do |m|%>
        <ul id="chat">
          <%= m.message %>
        </ul>
      <% end %>
    </div>

    <%= form_for Message.new, remote: true do |f| %>
      <%= f.text_field :message %>
      <%= f.submit "Send" %>
    <% end %>
    <%= subscribe_to "/messages/new" %>

I want to use a AJAX request to automatically update the messages and clear the text box. I tried using this but no luck

    <% publish_to "/messages/new" do %>
      $("#chat").append("<%= j render(@message) %>");
      $("#new_message")[0].reset();
    <% end %>

Any Ideas?

I see you've taken this code from Chatroom element won't update with sent messages until refreshed

Have a read through the comments there and let us know your outcome.

    <% publish_to "/messages/new" do %>
      $("#chat").append("<ul class='message'><%= @message.message %></ul>");
    <% end %>
      $("#new_message")[0].reset();