M. Hartl的Ruby on Rails教程。第10.2.1章 - 未定义的局部变量或方法`micropost'

时间:2012-11-21 02:12:11

标签: ruby-on-rails

我在第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 个答案:

答案 0 :(得分: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 %>

答案 1 :(得分:0)

在你看来,我看到有时你指的是微博:

micropost.content

以及其他作为类属性:

@micropost

你应该检查一下。

答案 2 :(得分:0)

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

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

现在,您的第micropost行中的本地变量<%= micropost.content %>将存在。

答案 3 :(得分:0)

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