Ruby on Rails Tutorial by M. Hartl. Chapter 10.2.1 - undefined local variable or method `micropost&#


I keep getting the following error in chapter 10.2.1 - undefined local variable or method `micropost'. I'm trying to get all the posts to render on the show page.

Here is my users controller:

class UsersController < ApplicationController before_filter :signed_in_user, only: [:index, :edit, :update, :show, :destroy] before_filter :correct_user, only: [:edit, :update] before_filter :admin_user, [:destroy] def index #@users = User.all @users = User.paginate(page: params[:page]) end def new @user = User.new end def destroy @user = User.find(params[:id]).destroy flash[:success] = "User has been deleted!" redirect_to users_path end def show @user = User.find(params[:id]) @microposts = @user.microposts.paginate(page: params[:page]) end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) if @user.update_attributes(params[:user]) #good! sign_in @user flash[:success] = "Profile" redirect_to user_path(@user) else render 'edit' end end def create @user = User.new(params[:user]) if @user.save sign_in @user flash[:success] = "Welcome to the Ruby app!" redirect_to user_path(@user) else render 'new' end end private def signed_in_user if !signed_in? store_location flash[:error] = "Please sign in!" redirect_to signin_url end end def correct_user @user = User.find(params[:id]) if !current_user?(@user) redirect_to '/' end end def admin_user if current_user.admin? #nothing else redirect_to '/' end end end

Here is my view

<% provide(:title, @user.name) %> <div class="row"> <aside class="span4"> <section>

<%= gravatar_for @user %> <%= @user.name %>

</section> </aside> <div class="span8"> <% if @user.microposts.any? %> <h3>Microposts (<%= @user.microposts.count %>)</h3> <ol class="microposts"> <li> <span class="content"><%= micropost.content %></span> <span class="timestamp"> Posted <%= time_ago_in_words(micropost.created_at) %> ago. </span> </li> </ol> <%= will_paginate @microposts %> <% end %> </div> </div>

Here is the micropost model:

# == Schema Information # # Table name: microposts # # id :integer not null, primary key # content :string(255) # user_id :integer # created_at :datetime not null # updated_at :datetime not null # class Micropost < ActiveRecord::Base attr_accessible :content belongs_to :user validates :content, presence: true, length: { maximum: 140 } validates :user_id, presence: true default_scope order: 'microposts.created_at DESC' end

Lastly, here is the users model:

# == Schema Information # # Table name: users # # id :integer not null, primary key # name :string(255) # email :string(255) # created_at :datetime not null # updated_at :datetime not null # password_digest :string(255) # remember_token :string(255) # admin :boolean default(FALSE) # class User < ActiveRecord::Base attr_accessible :name, :email, :password, :password_confirmation has_secure_password has_many :microposts, dependent: :destroy before_save { |user| user.email = email.downcase } before_save :create_remember_token validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } validates :password, presence: true, length: { minimum: 6 } validates :password_confirmation, presence: true private def create_remember_token self.remember_token = SecureRandom.urlsafe_base64 end end

Please let me know if you'd like me to post anymore code. Thanks!


In your view you are not looping the @user.microposts, try something like:

<% if @user.microposts.any? %> <h3>Microposts (<%= @user.microposts.count %>)</h3> <ol class="microposts"> <% @user.microposts.each do |micropost| %> <li> <span class="content"><%= micropost.content %></span> <span class="timestamp"> Posted <%= time_ago_in_words(micropost.created_at) %> ago. </span> </li> <% end %> </ol> <%= will_paginate @microposts %> <% end %>


In your view I'm seeing that sometimes you are refering to micropost as:


and in other ones as a class attribute:


You should check on that.


It looks like your view is missing a line that loops through the users microposts to display them. Add the line:

<% @microposts.each do |micropost| %>

Now the local variable micropost in your line <%= micropost.content %> will exist.


if you want to use the local variable (micropost) directly like micropost.content, then you have to make a partial (html.erb) file and call that file in the view with render method


  • can't convert nil into an exact number
  • MVC Google Map Polylines
  • Rails: redirect two pages back
  • Google Map API v3 example from their site doesn't work without an API key but does with a Key.
  • SQLite DB accessed from multiple threads
  • mysql regex_replace : how to use regex group in replacement
  • help me please with a Popup
  • Match Not working Excel: Error 1004 Unable to get the Match Property
  • Transferring a Java class between applications via serialization
  • Handling back button in Android Navigation Component
  • Regular expression (PCRE) for URL matching
  • Group files in a directory based on their prefix
  • Change error highlight color in vs code
  • DOMDocument::getElementById returns NULL [duplicate]
  • droppable in droppable
  • Google Street View container inside Shiny Application
  • argumentnullexception bitmap save to memorystream
  • Symfony YAML format conversion
  • current user id saved from _form or controller
  • Embedded or referenced relations
  • WPF ClickOnce missed references
  • Download/Save/Write a file on the client's hard disk using flash/flex
  • CSS: How to fix overlapping divs
  • How do I capture a rsolr Sunspot exception raised on a different thread from a model callback?
  • How to access recipient on sent messages page with mailboxer
  • SSL client cert authentication for only some URLs?
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • Configure nginx to return different files to different authenticated users with the same URI
  • VSCode change debug shell to bash on windows
  • How to override value that appears in a dropdown in the rails_admin gem
  • Ajax calls do not work in IE unless you fiddle with security settings
  • Is it possible to access block's scope in method?
  • req.body is undefined - nodejs
  • Javascript + PHP Encryption with pidCrypt
  • Symfony2: How to get request parameter
  • Akka Routing: Reply's send to router ends up as dead letters
  • Is there a mandatory requirement to switch app.yaml?
  • Getting Messege Twice Using IMvxMessenger
  • How can i traverse a binary tree from right to left in java?
  • How can I use threading to 'tick' a timer to be accessed by other threads?