2016-07-18 4 views
0

私はこの問題を解決しようとスタックオーバーフローのすべてを見ていると私は修正を見つけることができません。必要に応じてより多くのコードを含めることができます。未定義メソッド `each 'はnil:NilClassです。ビューとコントローラが一致する

コメントにコメントするためにコントローラを変更しようとしましたが、何も変わりません。

コメントするコメントに変更しようとしましたが、何も変わりません。

私はこれに固執しており、なぜこれが起こっているのか知りたいのです。おかげ

Comments_controller

class CommentsController < ApplicationController 
    before_action :set_comment, only: [:show, :edit, :update, :destroy] 

    # GET /comments 
    # GET /comments.json 

    # POST /comments 
    # POST /comments.json 
    def create 
    @pin = Pin.find(params[:pin_id]) 
    @comments = @pin.comments.new(comment_params) 
    @comment.user = current_user 

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

    # PATCH/PUT /comments/1 
    # PATCH/PUT /comments/1.json 
    # DELETE /comments/1 
    # DELETE /comments/1.json 
    def destroy 
    @comment.destroy 
    respond_to do |format| 
     format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_comment 
     @comment = Comment.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def comment_params 
     params.require(:comment).permit(:pin_id, :body, :user_id) 
    end 
end 

マイindex.html.erb

<h1>Listing comments</h1> 

<table> 
    <thead> 
    <tr> 
     <th>Link</th> 
     <th>Body</th> 
     <th>User</th> 
     <th colspan="3"></th> 
    </tr> 
    </thead> 

    <tbody> 
    <% @comments.each do |comment| %> 
     <tr> 
     <td><%= comment.link_id %></td> 
     <td><%= comment.body %></td> 
     <td><%= comment.user %></td> 
     <td><%= link_to 'Show', comment %></td> 
     <td><%= link_to 'Edit', edit_comment_path(comment) %></td> 
     <td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

<br> 

<%= link_to 'New Comment', new_comment_path %> 
+2

私は、コントローラのインデックスメソッドが欠落していると思いますか? – uzaif

+0

ありがとうございます。私はそれを逃したと信じていない。 – Auriga

答えて

3

は、コントローラにインデックスメソッドを追加します。

def index 
    @comments = Comment.all 

    respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @comments } 
    end 
end 
関連する問題