I'm following the Michael Hartl's Rails tutorial (http://ruby.railstutorial.org/chapters/user-microposts#code-home_with_feed) and I'm in chapter 10.36 where we are creating feeds of microposts to be shown on the Home page of the user. When I get to section 10.41 I should be able to see all of the posts on the home page but I keep getting an
NoMethodError in StaticPagesController#home
private method `feed' called for #<User:0x007f881cf77410>
if signed_in?
@micropost = current_user.microposts.build if signed_in?
@feed_items = current_user.feed.paginate(page: params[:page]) <-- error line
end
end
How do I get User.feed method to not be private so I can use in in the home page?
The code for user.rb
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
before_save { self.email = email.downcase }
before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum: 6 }
end
def feed
# This is preliminary. See "Following users" for the full implementation.
Micropost.where("user_id = ?", id)
end
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.digest(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = User.digest(User.new_remember_token)
end
The code for the static_page controller
class StaticPagesController < ApplicationController
def home
if signed_in?
@micropost = current_user.microposts.build if signed_in?
@feed_items = current_user.feed.paginate(page: params[:page])
end
end
def help
end
def about
end
def contact
end
end
The code for the home page view
<% if signed_in? %>
<div class="row">
<aside class="span4">
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<%= render 'shared/micropost_form' %>
</section>
</aside>
<div class="span8">
<h3>Post Feed</h3>
<%= render 'shared/feed' %>
</div>
</div>
'shared/feed' file
<% if @feed_item.any %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
<% end %>
As per the error private method
feed' called for #`,
You are calling a private
method named feed
on an instance of User
model.
In order to get the below code working
@feed_items = current_user.feed.paginate(page: params[:page])
Make sure that method feed
in User
model is not under private
section.
Just remember, a private method (i.e. a method declared outside of any class) can't be called on other objects.
UPDATE
You closed the User
class before the defined methods. Hence, the error.
class User < ActiveRecord::Base
## ...
validates :password, length: { minimum: 6 }
end ## REMOVE THIS end
def feed
## ...
private
def create_remember_token
self.remember_token = User.digest(User.new_remember_token)
end
end ## ADD THIS end