我在第 10.2.1 章中不断收到以下错误 - 未定义的局部变量或方法“micropost”。我正在尝试让所有帖子在显示页面上呈现。
这是我的用户控制器:
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
这是我的看法
<% provide(:title, @user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</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>
这是微博模型:
# == 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
最后,这是用户模型:
# == 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
如果您希望我发布更多代码,请告诉我。谢谢!