2016-07-18 7 views
0

Ich habe überall auf Stack-Überlauf geschaut, um zu versuchen, dieses Problem zu beheben, und ich kann nicht scheinen, eine Lösung zu finden. Ich kann bei Bedarf mehr Code hinzufügen.undefinierte Methode `each` für nil: NilClass. Die Ansichten und der Controller entsprechen

Ich habe versucht, in der Steuerung in Kommentare zu kommentieren und das ändert nichts.

Ich habe versucht, zu Kommentaren zu kommentieren, und das ändert nichts.

Ich bin legit auf diesem fest und ich möchte wissen, warum das passiert. Dank

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 

Mein 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

ich denke, Index-Methode in der Steuerung fehlt? – uzaif

+0

Danke. Ich kann nicht glauben, dass ich das verpasst habe. – Auriga

Antwort

3

Fügen Sie einen Index-Methode an die Steuerung:

def index 
    @comments = Comment.all 

    respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @comments } 
    end 
end 
Verwandte Themen