0

我在第 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

如果您希望我发布更多代码,请告诉我。谢谢!

4

4 回答 4

2

在您看来,您没有循环@user.microposts,请尝试以下操作:

      <% 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 %>
于 2012-11-21T02:32:41.213 回答
0

如果您想像 micropost.content 一样直接使用局部变量(micropost),那么您必须制作一个部分(html.erb)文件并使用 render 方法在视图中调用该文件

于 2013-11-19T02:20:51.290 回答
0

在您看来,我看到有时您将微博称为:

micropost.content

在其他的作为类属性:

@micropost

你应该检查一下。

于 2012-11-21T02:17:08.257 回答
0

看起来您的视图缺少一条循环通过用户微博以显示它们的行。添加行:

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

micropost现在您的行中的局部变量<%= micropost.content %>将存在。

于 2012-11-21T02:29:34.993 回答