我使用Ajax在我的文章顯示頁面創建評論,它的作品完美。現在,我想通過使用Ajax來顯示評論的錯誤。不過,現在我得到的錯誤:未定義的方法錯誤爲零:NilClass的評論
undefined method `errors' for nil:NilClass
我增添了新的方法,在我comments_controller.rb以確保應用程序知道實例變量註釋確實存在,但問題依然存在。任何建議,將不勝感激。
的意見/ create.js.erb:
$('#comments').append("<%= j render @comment %>").hide().fadeIn('slow');
// Empty the form
$('#comment_name').val("");
$('#comment_body').val("");
<% if @comment.errors.any? %>
(".comments-section").html('<%= j(render :partial => 'shared/errors', :object => comment) %>');
<% end %>
comment.rb:
comments_controller.rb:
class CommentsController < ApplicationController
before_action :set_article
before_action :set_comment, only: [:destroy]
before_action :login_like, only: [:upvote, :downvote]
def new
@comment = @article.comments.build
end
def create
@comment = @article.comments.create(params[:comment].permit(:name, :body))
if @comment.save
respond_to do |format|
format.html { redirect_to(@article) }
format.js # render 'create.js.erb'
end
else
respond_to do |format|
format.html { redirect_to(@article) }
format.js { render status: :500 }
end
end
end
def destroy
if @comment.destroy
respond_to do |format|
format.html { redirect_to(article_path(@article)) }
format.js
end
end
end
def upvote
if @comment.upvote_from current_user
respond_to do |format|
format.html { redirect_to(:back) }
format.js
end
end
end
def downvote
if @comment.downvote_from current_user
respond_to do |format|
format.html { redirect_to(:back) }
format.js
end
end
end
private
def set_article
@article = Article.find(params[:article_id])
end
def set_comment
@comment = @article.comments.find(params[:id])
end
def login_like
unless user_signed_in?
flash[:danger] = 'You must log in to like or dislike comments!'
end
end
end
組的意見/ _form.html.erb:
<%= render 'shared/errors', obj: @comment %>
<%= simple_form_for([@article, @article.comments.build], remote: true) do |f| %>
<div class="form-group row">
<div class="col-sm-2">
<%= f.label :name, class: "form-control-label" %>
</div>
<div class="col-sm-4">
<%= f.text_field :name, class: "form-control", placeholder: "Anyname..." %>
</div>
</div>
<div class="form-group row">
<div class="col-sm-2">
<%= f.label :body, class: "form-control-label" %>
</div>
<div class="col-sm-10">
<%= f.text_area :body, rows: 10, class: "form-control", placeholder: "Write a comment..." %>
</div>
</div >
<br>
<div class="form-group row">
<div class="col-sm-offset-2 col-sm-10">
<%= f.submit class: "btn btn-secondary" %>
</div>
</div>
<% end %>
的意見/共享/ _errors.html.erb:
<% if obj.errors.any? %>
<div class="row">
<div class="col-sm-9 col-sm-offset-3">
<div class="card">
<div class="card-header card-inverse card-danger">
<div class="card-title">
<%= pluralize(obj.errors.count, "error") %> prohibited this article from being saved:
</div>
<div class="card-text">
<ul>
<% obj.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
</div>
</div>
</div>
</div>
<% end %>
文章/ show.html.erb:
<div class="card col-sm-8 col-sm-offset-2 comments-section glassy-comment">
<h2 style="padding-top: 7px;">
Comment
</h2>
<hr>
<div id="comments">
<%= render @article.comments %>
</div>
<h3>Add a comment:</h3>
<div class="comment-wrapper">
<%= render 'comments/form' %>
</div>
</div>
</div>
你能告訴你怎麼創建中的文章顯示頁面的評論? – Pavan
@Pavan,是的,我剛剛更新了articles/show.html.erb文件,請看看。 –