【问题标题】:Hartl's Ruby on Rails tutorial ch 10.3.3: undefined method `any?' for nil:NilClassHartl 的 Ruby on Rails 教程第 10.3.3 章:未定义的方法 `any?'对于零:NilClass
【发布时间】:2014-06-26 22:14:31
【问题描述】:

卡在第 10 章 Hartl 的教程上。有错误:

  1)
<pre>Micropost pages micropost creation with invalid information should not create a micropost
     Failure/Error: expect { click_button "Post" }.not_to change(Micropost, :count)
     ActionView::Template::Error:
       undefined method `any?' for nil:NilClass
     # ./app/views/shared/_feed.html.erb:1:in `_app_views_shared__feed_html_erb__934824114_93581680'
     # ./app/views/static_pages/home.html.erb:14:in `_app_views_static_pages_home_html_erb___342719359_93055030'
     # ./app/controllers/concerns/microposts_controller.rb:10:in `create'
     # ./spec/requests/micropost_pages_spec.rb:16:in `block (5 levels) in <top (required)>'
     # ./spec/requests/micropost_pages_spec.rb:16:in `block (4 levels) in <top (required)>'
</pre>

  2) 
<pre>Micropost pages micropost creation with invalid information error messages 
     Failure/Error: before { click_button "Post" }
     ActionView::Template::Error:
       undefined method `any?' for nil:NilClass
     # ./app/views/shared/_feed.html.erb:1:in `_app_views_shared__feed_html_erb__934824114_93581680'
     # ./app/views/static_pages/home.html.erb:14:in `_app_views_static_pages_home_html_erb___342719359_93055030'
     # ./app/controllers/concerns/microposts_controller.rb:10:in `create'
     # ./spec/requests/micropost_pages_spec.rb:20:in `block (5 levels) in <top (required)>'
</pre>

_feed.html.erb:

<% if @feed_items.any? %>
  <ol class="microposts">
    <%= render partial: 'shared/feed_item', collection: @feed_items %>
  </ol>
  <%= will_paginate @feed_items %>
<% end %>

home.html.erb

    <% if signed_in? %>
  <div class="row">
    <aside class="span4">
      <section>
        <%= render 'shared/user_info' %>
      </section>
      <section>
        <%= render 'shared/micropost_form' %>
      </section>
    </aside>
    <div class="span8">
      <h3>Micropost Feed</h3>
      <%= render 'shared/feed' %>
    </div>
  </div>
<% else %>

microposts_controller.rb

    class MicropostsController < ApplicationController
  before_action :signed_in_user

  def create
    @micropost = current_user.microposts.build(micropost_params)
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_url
    else
      @feed_items = []      
      render 'static_pages/home'
    end
  end

  def destroy
    @micropost.destroy
    redirect_to root_url
  end

  private

    def micropost_params
      params.require(:micropost).permit(:content)
    end

    def correct_user
      @micropost = current_user.microposts.find_by(id: params[:id])
      redirect_to root_url if @micropost.nil?
    end
end

micropost_pages_spec.rb

    require 'spec_helper'

describe "Micropost pages" do

  subject { page }

  let(:user) { FactoryGirl.create(:user) }
  before { sign_in user }

  describe "micropost creation" do
    before { visit root_path }

    describe "with invalid information" do

      it "should not create a micropost" do
        expect { click_button "Post" }.not_to change(Micropost, :count)
      end

      describe "error messages" do
        before { click_button "Post" }
        it { should have_content('error') }
      end
    end

    describe "with valid information" do

      before { fill_in 'micropost_content', with: "Lorem ipsum" }
      it "should create a micropost" do
        expect { click_button "Post" }.to change(Micropost, :count).by(1)
      end
    end
  end
end

static_pages_conroller.rb

class StaticPagesController < ApplicationController

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

def about
end

def help
end

def contact
end

end

这是我第一次在 stackoverflow 上发帖。如有错误请见谅 如果您需要更多信息,请提出问题。我在 ch 中收到了这个错误。 10.3.3 Harlt 的 Rails 教程

【问题讨论】:

  • 您是否将您的代码与参考代码github.com/railstutorial/sample_app_rails_4进行了比较
  • 你的静态控制器主页动作怎么样?它应该定义@feed_items
  • 添加带有主页操作的 static_pages_conroller.rb
  • 错误不是来自静态页面控制器-/app/controllers/concerns/microposts_controller.rb:10:in 'create'
  • 您的文件夹结构有问题 - 您的 MicropostsController 不应位于 concerns 文件夹中,错误指向的行是第 10 行,即 @feed_items = []?

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


【解决方案1】:

它是 2 个 microposts_controller.rb ,其中一个在文件夹 app/controllers/concerns 中 文件位于 app/controllers/ - 拼写错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多