2017-06-14 32 views
0

我试图在Rails 5上呈现create.js.erb,当我在应用程序中调用创建注释动作时,我无法使其工作,并且我正在耗尽时间,我会非常感谢你的帮助。这是我的代码:RAILS 5 Javascript无法在远程真正的方法

我Comments_Controller(一部分):

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!!!"); 
+0

控制台上也没有错误 –

+0

当您提交表单时会发生什么? – Iceman

+0

它刷新页面并创建评论,一切正常,但.js.erb whis完全被忽略 –

回答

0

这是与Windows相关的错误,我将我的代码移至Ubuntu,现在.js.erb文件完美工作