Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails: undefined method "comments" for nil:NilClass
    primarykey
    data
    text
    <p>I am developing an app with users where they each have set of microposts displayed on their pages. I am trying to add comments to these microposts. Every time I visit localhost:3000/users/(user_id_#)</p> <p>I get this error: undefined method `comments' for nil:NilClass</p> <p>This error only comes when the user has microposts to show. Otherwise it just shows their blank page. The error comes from this view for app/views/users/show.html.erb This view renders this partial, where the error occurs in line 13. </p> <pre><code>&lt;li&gt; &lt;span class="content"&gt;&lt;%= micropost.content %&gt;&lt;/span&gt; &lt;span class="timestamp"&gt; Posted &lt;%= time_ago_in_words(micropost.created_at) %&gt; ago. &lt;/span&gt; &lt;% if current_user?(micropost.user) %&gt; &lt;%= link_to "delete", micropost, method: :delete, confirm: "You sure?", title: micropost.content %&gt; &lt;% end %&gt; &lt;h2&gt;Comments&lt;/h2&gt; &lt;% micropost.comments.each do |comment| %&gt; &lt;p&gt; &lt;b&gt;Commenter:&lt;/b&gt; &lt;%= comment.commenter %&gt; &lt;/p&gt; &lt;p&gt; &lt;b&gt;Comment:&lt;/b&gt; &lt;%= comment.body %&gt; &lt;/p&gt; &lt;% end %&gt; &lt;h3&gt;Add a comment:&lt;/h3&gt; &lt;%= form_for([micropost, micropost.comments.build]) do |f| %&gt; &lt;div class="field"&gt; &lt;%= f.label :commenter %&gt;&lt;br /&gt; &lt;%= f.text_field :commenter %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= f.label :body %&gt;&lt;br /&gt; &lt;%= f.text_area :body %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;/li&gt; </code></pre> <p>Here is my comment.rb file</p> <pre><code>class Comment &lt; ActiveRecord::Base belongs_to :micropost attr_accessible :body, :user_id end </code></pre> <p>and my micropost.rb file</p> <pre><code>class Micropost &lt; ActiveRecord::Base attr_accessible :content belongs_to :user has_many :comments validates :content, presence: true, length: { maximum: 140 } validates :user_id, presence: true end </code></pre> <p>and my comments_controller.rb</p> <pre><code>class CommentsController &lt; ApplicationController def create @micropost = Micropost.find(params[:micropost_id]) @comment = @micropost.comments.create(params[:comment]) redirect_to micropost_path(@micropost) end end </code></pre> <p>and finally my microposts_controller.rb</p> <pre><code>class MicropostsController &lt; ApplicationController before_filter :signed_in_user before_filter :correct_user, only: :destroy def create @micropost = current_user.microposts.build(params[:micropost]) if @micropost.save flash[:success] = "Micropost created!" redirect_to root_path else @feed_items = [] render 'static_pages/home' end end def new @micropost = Micropost.new(params[:micropost]) end def show @micropost = Micropost.find(params[:id]) end def destroy @micropost.destroy redirect_back_or root_path end private def correct_user @micropost = current_user.microposts.find_by_id(params[:id]) redirect_to root_path if @micropost.nil? end end class CommentsController &lt; ApplicationController def create @micropost = Micropost.find(params[:micropost_id]) @comment = @micropost.comments.create(params[:comment]) redirect_to micropost_path(@micropost) end end </code></pre> <p>also here is the users_controller.rb</p> <pre><code>class UsersController &lt; ApplicationController before_filter :signed_in_user, only: [:index, :edit, :update, :destroy, :following, :followers] before_filter :correct_user, only: [:edit, :update] before_filter :admin_user, only: :destroy def index @users = User.paginate(page: params[:page]) end def show @user = User.find(params[:id]) @micropost = @user.microposts.first @comment = Comment.new end def new @user = User.new end def create @user = User.new(params[:user]) if @user.save sign_in @user flash[:success] = "Welcome to the Sample App!" redirect_to @user else render 'new' end end def edit end def update if @user.update_attributes(params[:user]) flash[:success] = "Profile updated" sign_in @user redirect_to @user else render 'edit' end end def destroy User.find(params[:id]).destroy flash[:success] = "User destroyed." redirect_to users_path end def following @title = "Following" @user = User.find(params[:id]) @users = @user.followed_users.paginate(page: params[:page]) render 'show_follow' end def followers @title = "Followers" @user = User.find(params[:id]) @users = @user.followers.paginate(page: params[:page]) render 'show_follow' end private def correct_user @user = User.find(params[:id]) redirect_to(root_path) unless current_user?(@user) end def admin_user redirect_to(root_path) unless current_user.admin? end end </code></pre> <p>from the error it sounds like the @micropost isnt being initialized in the microposts_controller.rb file under def show. but I think it is? What am I doing wrong? Thanks </p> <p>also here is app/views/users/show.html.erb</p> <pre><code>&lt;% provide(:title, @user.name) %&gt; &lt;div class="row"&gt; &lt;aside class="span4"&gt; &lt;section&gt; &lt;h1&gt; &lt;%= gravatar_for @user %&gt; &lt;%= @user.name %&gt; &lt;/h1&gt; &lt;/section&gt; &lt;/aside&gt; &lt;div class="span8"&gt; &lt;%= render 'follow_form' if signed_in? %&gt; &lt;% if @user.microposts.any? %&gt; &lt;h3&gt;Microposts (&lt;%= @user.microposts.count %&gt;)&lt;/h3&gt; &lt;ol class="microposts"&gt; &lt;%= render @microposts %&gt; &lt;/ol&gt; &lt;%= will_paginate @microposts %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload