繁体   English   中英

在Rails教程中删除ruby中的micropost

[英]deleting micropost in ruby on rails tutorial

我一直在研究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)

使用者似乎没有要删除的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

我想到了。 microposts_controller中的一个简单错字。

该行:

redirect_to root_url if @microposts.nil?

应该

redirect_to root_url if @micropost.nil?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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