0

M.Hart の Rails チュートリアルを実行してきました。セクションの 1 つで、自分のものではないユーザーごとに、ユーザー インデックス ページに削除リンクを実装します。あなたが管理者ユーザーである場合。また、各ユーザーのプロフィール ページに削除リンクを実装したいと考えています。リンクの削除コードをパーシャルに入れましたが、インデックス ページと個々のユーザーの表示ページで正常に動作していますが、パーシャルを表示ページのコードに追加した後も 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 (部分 -> _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 -> 以下のコードは、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 show および index アクションと before フィルター

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

この応答があなたに伝えていることは、マスターを呼び出すときですか? current_user オブジェクトでは、current_user オブジェクトは Nil であり、存在しないことを意味します。

これは、@current_user が現在定義されておらず、User.find もユーザーを見つけていないことを意味します。

@user をパーシャルに渡しているので、条件は @user に基づいている必要があると思います。

これが理にかなっていることを願っています

于 2013-04-26T03:50:31.400 に答える