坚持第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教程
答案 0 :(得分:0)
这是2个microposts_controller.rb,其中一个是文件夹app / controllers / Concer 文件位于app / controllers / - 拼写错误。