RAILS 5 Javascript无法使用远程true方法

时间:2017-06-14 17:12:49

标签: javascript jquery html ruby-on-rails ajax

当我在我的应用程序上调用创建注释操作时,我试图在Rails 5上渲染一个create.js.erb,我无法让它工作而且我已经没时间了,我会非常感谢你的帮助。这是我的代码:

我的评论_控制器(部分内容):

class CommentsController < ApplicationController
  before_action :set_comment, :set_replies, only: [:show, :edit, :update,:destroy]
  before_action :set_congress, :set_category, :set_presentation
  before_action :authenticate_user!, except: [:index, :show]
  respond_to :js, :html
  layout "insideapplication"
  # GET /comments
  # GET /comments.json
  def index
    @comments = Comment.all
    @comment = Comment.new
  end

  def new
    @comment = Comment.new(:parent_id => params[:parent_id])
  end

 def create
   @comment = Comment.new(comment_params)
   @comment.author_id = current_user.id
   @comment.presentation = @presentation

   respond_to  do |format|
      if @comment.save
        format.html { redirect_to  [@congress, @category, @presentation],notice: 'Comment was successfully created.' }
        format.json { render :show, status: :created, location: @comment }
        format.js 
      else
        format.html { render :new }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
        format.js 
      end
    end
  end

我的index.html.erb(评论)

<div class="container">
    <div class="row">
      <div class="text-center">
          <!-- Button trigger modal -->
          <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#mynewpost">
            New Post
          </button>
      </div>
    </div>

    <br>
    <hr>
    <div class="row" id="container_posts">
        <%= render @posts %>
    </div>
    <!-- Modal  create action -->
      <%= form_for(@post, remote: true) do |f| %>
          <div class="modal fade" id="mynewpost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <h4 class="modal-title" id="myModalLabel">Post</h4>
            </div>
            <div class="modal-body">
              <div class="field">
                <%= f.label :title %><br>
                <%= f.text_area :title, class: "form-control post_title" %>
              </div>
              <div class="field">
                <%= f.label :content %><br>
                <%= f.text_area :content, class: "form-control post_content" %>
              </div>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal" id="mynewpostclose">Close</button>
              <%= submit_tag "Create", class: "btn btn-primary" %>
            </div>
          </div>
        </div>
        </div>
      <% end %>
    <!-- Modal -->
</div>

我的create.js.erb(仅测试js)

alert("test!!!");

1 个答案:

答案 0 :(得分:0)

此接缝是与Windows相关的错误,我将代码移动到Ubuntu,现在.js.erb文件工作正常