我一直在运行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