我一直在研究Hartl的教程,当我遇到这个错误时差不多完成了第10章:
1) MicropostPages micropost destruction as correct user should delete a micropost
Failure/Error: expect { click_link "delete" }.to change(Micropost, :count).by(-1)
count should have been changed by -1, but was changed by 0
# ./spec/requests/micropost_pages_spec.rb:38:in `block (4 levels) in <top (required)>'
当我点击应用程序本身中的“删除”链接时,它会重定向而不删除微博。我来回比较我的代码和他的在线搜索,但我似乎无法找到问题。我希望你们中有一个比我更有经验的人可以提供帮助。
规格/请求/ micropost_pages_spec.rb:
describe "micropost destruction" do
before { FactoryGirl.create(:micropost, user: user) }
describe "as correct user" do
before { visit root_path }
it "should delete a micropost" do
expect { click_link "delete" }.to change(Micropost, :count).by(-1)
end
end
end
microposts_controller.rb:
class MicropostsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy]
before_action :correct_user, only: [:destroy]
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 @microposts.nil?
end
end
应用程序/视图/微柱/ _micropost.html.erb:
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
data: { confirm: "You sure?" },
title: micropost.content %>
<% end %>
</li>
应用程序/视图/共享/ _feed_item.html.erb:
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user%>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at)%> ago.
</span>
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete,
data: { confirm: "You sure" },
title: feed_item.content %>
<% end %>
</li>
配置/ routes.rb中:
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
当我点击“删除”链接
时,我得到了这个Started DELETE "/microposts/295" for 127.0.0.1 at 2014-07-01 16:02:34 -0400
Processing by MicropostsController#destroy as HTML
Parameters: {"authenticity_token"=>"mQUxCMrrNfelpeXmxqO0+GmznKS7BGQeFL5upFgp0Tc=", "id"=>"295"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = '99409d871e582f2e3251d83f33751645707c72bc' LIMIT 1
Micropost Load (0.6ms) SELECT "microposts".* FROM "microposts" WHERE "microposts"."user_id" = ? AND "microposts"."id" = 295 ORDER BY created_at DESC LIMIT 1 [["user_id", 1]]
Redirected to http://localhost:3000/
Filter chain halted as :correct_user rendered or redirected
Completed 302 Found in 14ms (ActiveRecord: 0.9ms)
答案 0 :(得分:0)
几乎看起来用户没有要删除的Micropost?在执行规范之前,请确保有一个设置。您可以添加:expect(user.microposts.count).to_not eq(0),如下所示,以确认有属于该用户的帖子。
describe "micropost destruction" do
before { FactoryGirl.create(:micropost, user: user) }
describe "as correct user" do
before { visit root_path }
it "should delete a micropost" do
expect(user.microposts.count).to_not eq(0)
expect { click_link "delete" }.to change(Micropost, :count).by(-1)
end
end
end
答案 1 :(得分:0)
我明白了。 microposts_controller中的一个简单错字。
行:
redirect_to root_url if @microposts.nil?
应该是
redirect_to root_url if @micropost.nil?