简体   繁体   中英

deleting micropost in ruby on rails tutorial

I've been working through Hartl's tutorial and almost finished chapter 10 when I hit this error:

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)>'

When I click the "delete" link in the app itself, it redirects without deleting the micropost. I've gone back and forth comparing my code to his, and searching online, but I can't seem to find the problem. I am hoping that one of you with more experienced eyes than mine can help.

spec/requests/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

app/views/microposts/_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>

app/views/shared/_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>

config/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'

I get this when I click the "delete" link

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)

It almost looks like the user doesn't have a Micropost to delete? Make sure there is one setup before you execute the spec. You could add this: expect(user.microposts.count).to_not eq(0) as shown below to confirm there is a post belonging to the user.

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

I figured it out. A simple typo in the microposts_controller.

the line:

redirect_to root_url if @microposts.nil?

should be

redirect_to root_url if @micropost.nil?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM