0

我一直在阅读 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>

共享/user_delete_link(部分 -> _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 -> 下面的代码是如果将部分(以上)添加到用户/显示代码中的规范。当部分被删除时,这些规格会变回绿色

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

结尾

用户.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)>'

会话助手:

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

1 回答 1

0

这个响应告诉你的是,当它调用 master 时?在 current_user 对象上,current_user 对象为 Nil,表示它不存在。

这意味着 @current_user 当前没有定义,并且 User.find 也没有找到用户。

您将@user 传递给您的部分,所以我认为您的条件应该基于@user。

希望这是有道理的

于 2013-04-26T03:50:31.400 回答