我一直在運行M.Hart的Rails教程。在其中一個部分中,您可以爲每個不屬於您自己的用戶在用戶索引頁面上實施刪除鏈接;提供你是一個管理員用戶。我也想實現一個刪除鏈接到每個用戶的個人資料頁面。我把刪除鏈接代碼放入了部分內容中,並且它在索引頁面和單獨的用戶展示頁面上運行良好,但在將show部分添加到show page代碼後,我仍然收到RSpec失敗。我的代碼在下面...順便說一句 - 主用戶與管理員用戶相同。Rails RSpec MHart的教程用戶刪除鏈接
用戶/ 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 /刪除/ 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
末
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
如果您希望發佈任何代碼或添加更多信息,請讓我知道:)謝謝。 ANCE。
更新: 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
請張貼您的Rspec故障。 – 2013-04-21 12:37:47
完成:)上述rspec錯誤適用於所有11個規格失敗 – 2013-04-26 03:17:46
您的測試如何登錄,因此current_user已設置? – 2013-04-26 03:19:25