2016-03-07 50 views
-1

我使用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> 
+0

你能告訴你怎麼創建中的文章顯示頁面的評論? – Pavan

+0

@Pavan,是的,我剛剛更新了articles/show.html.erb文件,請看看。 –

回答

0

您顯示form添加評論在articles/show.html.erb,所以@comment需要在articles#show行動

#artcles_controller 

def show 
    @comment = Comment.new 
end 

定義,你需要將它在form通過爲local params到部分

<%= render 'comments/form', comment: @comment %> 

最後改變下面的線comments/_form.html.erb

<%= render 'shared/errors', obj: @comment %> 

<%= render 'shared/errors', obj: comment %> 
+0

非常感謝。它不再顯示任何錯誤。儘管當我嘗試提交空白評論時,它並未顯示我在評論模型中驗證的錯誤。我仍然將您的答案標記爲解決方案。祝你今天愉快。 –

+0

@FinalFantasyVIII評論模型中有哪些驗證?在你的問題中更新它。 – Pavan

+0

我剛剛更新了comment.rb,create.erb.js和我的comments_controller.rb文件。如果我錯了,請快速查看並糾正我。 –

1

您只在之前設置了@comment 210:

before_action :set_comment, only: [:destroy] 

,但你使用它也:upvotedownvote

+0

謝謝你的幫助。但是,我不太明白它是如何導致問題的。你能更具體嗎?順便說一下,upvote和downvote方法仍然可以正常工作。 –

相關問題