Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Rails Ajax不会重新加载_Javascript_Jquery_Ruby On Rails_Ajax - Fatal编程技术网

Javascript Rails Ajax不会重新加载

Javascript Rails Ajax不会重新加载,javascript,jquery,ruby-on-rails,ajax,Javascript,Jquery,Ruby On Rails,Ajax,创建实时聊天应用程序,但每当我发布消息时,我都必须重新加载页面才能看到我的新消息。聊天不会自行重新加载 _form.html.erb 不能为空或包含超过2000个符号。 _comments.html.erb 说 [现场] create.js.erb publisher=client.publish(“/comments”{ 消息:“” }); publisher.callback(函数(){ $('comment_body').val(''); $('new'u comment').

创建实时聊天应用程序,但每当我发布消息时,我都必须重新加载页面才能看到我的新消息。聊天不会自行重新加载

_form.html.erb

不能为空或包含超过2000个符号。
_comments.html.erb
  • 说 [现场]

  • create.js.erb
    publisher=client.publish(“/comments”{
    消息:“”
    });
    publisher.callback(函数(){
    $('comment_body').val('');
    $('new'u comment').find(“input[type='submit']”).val('submit').prop('disabled',false)
    });
    publisher.errback(函数(){
    警报('发布评论时出错');
    });
    

    编辑:添加注释。\u controller.rb
    class CommentsController
    您应该在此处发布控制器代码-如果不知道您是如何发送数据的,并且希望收到收据,我将无法帮助您。@RichPeck添加了控制器代码。谢谢你应该在这里发布你的控制器代码-如果不知道你是如何发送数据的,并且希望收到收据,我就帮不了你。@RichPeck添加了控制器代码。谢谢
    <div class="well">
      <%= form_for @comment, remote: true do |f| %>
        <div class="form-group">
          <%= f.label :body, 'Enter your comment:' %>
          <%= f.text_area :body, rows: 3, class: 'form-control', required: true, maxlength: 2000 %>
          <small class="label label-warning">Cannot be blank or contain more than 2000 symbols.</small>
        </div>
    
        <%= f.submit 'Post', class: 'btn btn-primary btn-lg' %>
      <% end %>
    </div>
    
    <li class="media comment">
      <%= link_to image_tag(comment.user.avatar_url, alt: comment.user.name, class: "media-object"),
                  comment.user.profile_url, target: '_blank', class: 'pull-left' %>
      <div class="media-body">
        <h4 class="media-heading"><%= link_to comment.user.name, comment.user.profile_url, target: '_blank' %> says
          <small class="text-muted">[at <%= comment.created_at.strftime('%-d %B %Y, %H:%M:%S') %>]</small></h4>
        <p><%= comment.body %></p>
      </div>
    </li>
    
    publisher = client.publish('/comments', {
      message: '<%= j render @comment %>'
    });
    
    publisher.callback(function() {
      $('#comment_body').val('');
      $('#new_comment').find("input[type='submit']").val('Submit').prop('disabled', false)
    });
    
    publisher.errback(function() {
      alert('There was an error while posting your comment.');
    });
    
    class CommentsController < ApplicationController
      def new
        @comment = Comment.new
        @comments = Comment.order('created_at DESC')
      end
    
      def create
        respond_to do |format|
          if current_user
            @comment = current_user.comments.build(comment_params)
            if @comment.save
              flash.now[:success] = 'Your comment was successfully posted!'
            else
              flash.now[:error] = 'Your comment cannot be saved.'
            end
            format.html {redirect_to root_url}
            format.js
          else
            format.html {redirect_to root_url}
            format.js {render nothing: true}
          end
        end
      end
    
      private
    
      def comment_params
        params.require(:comment).permit(:body)
      end
    end