【问题标题】:Ch. 10.3.3 Ruby on Rails Tutorial Undefined Method 'any?'通道。 10.3.3 Ruby on Rails 教程未定义方法“任何?”
【发布时间】:2013-06-17 20:10:06
【问题描述】:

当用户登录时,我试图将所有待办事项的提要放在首页上。但是,@feed_items 没有返回任何内容,即使提要正在工作。 这是错误:

未定义的方法“任何?”对于 nil:NilClass

这是提取的来源:

1: <% if @feed_items.any? %>
2:  <ol class="todos">
3:      <%= render partial: 'shared/feed_item', collection: @feed_items %>
4:  </ol>

这是我的静态页面控制器:

class StaticPagesController < ApplicationController

def home
    if signed_in?
        @todo = current_user.todos.build
        @feed_items = current_user.feed.paginate(page: params[:page])
    end
end
end

这是我的 User.rb

class User < ActiveRecord::Base
attr_accessible :email, :username, :password, :password_confirmation
has_secure_password

before_save { |user| user.email = email.downcase }
before_save :create_remember_token

has_many :todos, :dependent => :destroy

validates :username, :presence => true, length: { maximum: 50 }
validates :password, :presence => true, length: { minimum: 6 }
validates :password_confirmation, presence: true
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 }

def feed
  # This is preliminary. See "Following users" for the full implementation.
  Todo.where("user_id = ?", id)
end

private

def create_remember_token
  self.remember_token = SecureRandom.urlsafe_base64
end
end

这是我的主页 (index.html.erb)

% if signed_in? %>
<div class="row">
    <aside class="span4">
        <section>
            <%= render 'shared/user_info' %>
        </section>
    </aside>
    <div class="span8">
        <h3>Todo Feed</h3>
        <%= render 'shared/feed' %>
    </div>
</div>
<% else %>
<div class="center hero-unit">
    <h1>Welcome to the To Do app!</h1>

    <p>This app allows people to create a to do list for themselves on the web  browser! HELLLZZZ YEEAAAHHH!!!</p>

    <%= link_to "Sign Up Here!", signup_path, class: "btn btn-large btn-primary" %>
    <%= link_to "Find your Friends (and other Users) Here!", users_path, class: "btn btn-large btn-primary" %>

非常感谢您!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 railstutorial.org


    【解决方案1】:

    Nil 不提供方法any?。一个快速的技巧是使用try 方法:

    <% if @feed_items.try(:any?) %>
    

    【讨论】:

      【解决方案2】:

      @feed_items 为 nil,您正在尝试调用 any?。 Nil 没有那个方法。

      您的控制器是“家”,但您的视图是“索引”。可能是您的控制器操作未运行,因此未填充 @feed_items。尝试将home 重命名为index,看看是否能解决问题。

      【讨论】:

      • 所以两件事。我确实必须将我的方法重命名为索引以对应于正确的页面。然后我实际上有一个 Home 控制器,并意识到我将我的方法放入了错误的控制器中。感谢您的帮助!
      【解决方案3】:

      在创建操作中渲染“static_pages/home”之前,确保 MicropostsController 存在 @feed_items = []。

      【讨论】:

        【解决方案4】:

        请参阅 Hartl Rails 教程中的清单 11.48。您需要在 'todos' 控制器中的 'create' 方法中添加以下代码行:@feed_items = []。在添加此行之前,我遇到了同样的问题。现在一切正常。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-13
          • 2013-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多