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">×</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!!!");
控制檯上也沒有錯誤 –
當您提交表單時會發生什麼? – Iceman
它刷新頁面並創建評論,一切正常,但.js.erb whis完全被忽略 –