简体   繁体   中英

Rails Guide “undefined method 'comments' for nil:NilClass”

I am new to Rails and working through the Getting Started Guide . I read through this similar question about the guide but it doesn't seem relevant.

I am stuck on section 6.3, where we are trying to let users add comments to blog posts. I have added a comments form to the post show view, which was working fine before, but now raises the following error. What is the issue?

NoMethodError in Posts#show

Showing /Users/.../Desktop/Rails Blog/blog/app/views/posts/show.html.erb where line #24 raised: 

undefined method `comments' for nil:NilClass



Extracted source (around line #24):

    21 <% end %>
    22 
    23 <h2>Add a comment:</h2>
    24 <%= form_for([@post, @posts.comments.build]) do |f| %>
    25    <p>
    26         <%= f.label :commenter %><br />
    27         <%= f.text_field :commenter %>

posts_controller.rb:

class PostsController < ApplicationController
    def index
        @posts = Post.all
    end
    def new
        @post = Post.new
    end
    def create
        @post = Post.new(params[:post].permit(:title, :text))
        if @post.save
            redirect_to @post
        else
            render 'new'
        end
    end
    def edit
        @post = Post.find(params[:id])
    end
    def update
        @post = Post.find(params[:id])

        if @post.update(params[:post].permit(:title, :text))
            redirect_to @post
        else
            render 'edit'
        end
    end
    def show
        @post = Post.find(params[:id])
    end
    def destroy
        @post = Post.find(params[:id])
        @post.destroy
        redirect_to posts_path
    end
    private
        def post_params
            params.require(:post).permit(:title, :text)
        end
end

comments_controller:

class CommentsController < ApplicationController
    def create
        @post = Post.find(params[:post_id])
        @comment = @post.comments.create(params[:comment].permit(:commenter, :body))
        redirect_to post
    end
end

posts.show.html.erb:

<p>
    <strong>Title:</strong>
    <%= @post.title %>
</p>

<p>
    <strong>Text:</strong>
    <%= @post.text %>
</p>

<h2>Comments</h2>
<% @post.comments.each do |comment| %>
    <p>
        <strong>Commenter:</strong>
        <%= comment.commenter %>
    </p>
    <p>
        <strong>Comment:</strong>
        <%= comment.body %>
    </p>
<% end %>

<h2>Add a comment:</h2>
<%= form_for([@post, @posts.comments.build]) do |f| %>
    <p>
        <%= f.label :commenter %><br />
        <%= f.text_field :commenter %>
    </p>
    <p>
        <%= f.label :body %><br />
        <%= f.text_area :body %>
    </p>
    <p>
        <%= f.submit %>
    </p>
<% end %>

<%= link_to 'Back', posts_path %>
<%= link_to 'Edit', edit_post_path(@post) %>

You have @posts . The variable is called @post . Drop the "s".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM