Rails RSpec MHart的教程用户删除链接

时间:2013-04-21 09:14:23

标签: ruby-on-rails rspec specifications

我一直在浏览M.Hart的Rails教程。在其中一个部分中,您在用户索引页面上为每个不属于您自己的用户实现删除链接;为您提供管理员用户。我还想在每个用户的个人资料页面中实现删除链接。我把删除链接代码放到了一个部分,它在索引页面上正常运行,并且各个用户显示页面,但在将部分添加到显示页面代码后,我仍然遇到RSpec失败。我的代码如下...顺便说一句 - 主用户与管理员用户相同。

用户/ show.html.erb

<section>
    <%= gravatar_for @user %>
    <h3><%= @user.name %></h3>
    <div id="profile_delete_link"><%= render 'shared/user_delete_link', user: @user %></div>
</section>

shared / user_delete_link(partial - &gt; _user / delete / link.html.erb)

<% if !current_user?(user) && current_user.master? %>
    >> <%= link_to "delete", user, method: :delete, data: { confirm: "You sure?" } %>
<% end %>

user_pages_spec.rb - &gt;下面的代码是如果将部分(上面)添加到users / show代码中的规范。删除部分后,这些规格将恢复为绿色

describe "profile page" do
let(:user) { FactoryGirl.create(:user, name: "foo bar") }

let!(:mp1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
let!(:mp2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }

before { visit user_path(user) }

it { should have_selector('h3',    text: user.name) } 
it { should have_selector('title', content: "F.Bar") }

describe "microposts" do
  it { should have_content(mp1.content) }
  it { should have_content(mp2.content) }
  it { should have_content(user.microposts.count) }
end

user.rb

# == Schema Information
#
# Table name: users
#
#  id              :integer          primary key
#  name            :string(255)
#  email           :string(255)
#  password_digest :string(255)
#  created_at      :datetime
#  updated_at      :datetime
#  master          :boolean
#

class User < ActiveRecord::Base
has_secure_password 

email_regex = /^[_+a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i

attr_accessible :name, :email, :password, :password_confirmation
has_many :microposts, dependent: :destroy

    validates :name, :presence => true, :length => { within: 2..40 }
    validates :email, :presence => true, :uniqueness => { case_sensitive: false }, :format  => { with: email_regex }
    validates :password, :presence  => true, :length => { within: 5..50 }
    validates :password_confirmation, :presence => true

def feed
    Micropost.where("user_id = ?", id)
end
end

user_controller.rb显示和索引操作以及过滤器之前

before_filter :authorize,     only: [:index, :edit, :update, :destroy]
before_filter :correct_user,  only: [:edit, :update]
before_filter :master_user,   only: [:destroy]

def index
  @users = User.all
end

def show
  @user = User.find(params[:id])
  @microposts = @user.microposts.paginate(page: params[:page])
  @title = initials_of @user.name
end

如果您希望我发布任何代码或添加更多信息,请告诉我:)提前感谢。

更新: RSpec错误:

UserPages profile page microposts
 Failure/Error: before { visit user_path(user) }
 ActionView::Template::Error:
   undefined method `master?' for nil:NilClass
 # ./app/views/shared/_user_delete_link.html.erb:1:in `_app_views_shared__user_delete_link_html_erb__1484829579316662700_70204671481360'
 # ./app/views/users/show.html.erb:6:in `_app_views_users_show_html_erb___32313850444620306_70204671449340'
 # ./spec/features/user_pages_spec.rb:88:in `block (3 levels) in <top (required)>'

SessionsHelper:

module SessionsHelper
 ...........
    def current_user
        @current_user ||= User.find(session[:user_id]) if session[:user_id]
    end
 ...........
end

1 个答案:

答案 0 :(得分:0)

这个回复告诉你的是它什么时候叫主人?在current_user对象上,current_user对象为Nil,表示它不存在。

这意味着当前未定义@current_user,User.find也未找到该用户。

您正在将@user传递给您的部分,因此我认为您的条件应该基于@user。

希望这是有道理的