gpt4 book ai didi

ruby-on-rails - Ruby on Rails 教程第 11 章 : Rspec tests fail on follower/following section

转载 作者:太空宇宙 更新时间:2023-11-03 17:07:47 26 4
gpt4 key购买 nike

我正在研究 Chapter 11在 Hartl 的教程中,试图让用户 Controller 中的未登录用户访问关注者和关注页面通过。我遇到了一个错误,Rails 会提示未定义的 admin? 方法,尽管它只在 User 部分中使用。

Rails 本地服务器

Rendered users/_user.html.erb (9.4ms)
Rendered users/show_follow.html.erb within layouts/application (33.0ms)
Completed 500 Internal Server Error in 43ms

ActionView::Template::Error (undefined method `admin?' for nil:NilClass):
1: <li>
2: <%= gravatar_for user, size: 52 %>
3: <%= link_to user.name, user %>
4: <% if current_user.admin? && !current_user?(user) %>
5: | <%= link_to 'delete', user, method: :delete,
6: data: { confirm: 'Are you sure?' } %>
7: <% end %>
app/views/users/_user.html.erb:4:in `_app_views_users__user_html_erb___4000670281664801561_70099451578240'
app/views/users/show_follow.html.erb:25:in `_app_views_users_show_follow_html_erb__2024448610717183111_70099484108000'
app/controllers/users_controller.rb:68:in `followers'

RSpec 失败

 1) Authentication authorization for non-logged-in users in the Users controller visiting the following page
Failure/Error: it { should have_selector('title', text: 'Login') }
expected css "title" with text "Login" to return something
# ./spec/requests/authentication_pages_spec.rb:122:in `block (6 levels) in <top (required)>'

2) Authentication authorization for non-logged-in users in the Users controller visiting the followers page
Failure/Error: it { should have_selector('title', text: 'Login') }
expected css "title" with text "Login" to return something
# ./spec/requests/authentication_pages_spec.rb:127:in `block (6 levels) in <top (required)>'

authorization_pages_spec.rb

  describe "authorization" do
.
.
.
describe "for non-logged-in users" do
let(:user) { FactoryGirl.create(:user) }
.
.
.
describe "in the Users controller" do
.
.
.
describe "visiting the following page" do
before { visit following_user_path(user) }
it { should have_selector('title', text: 'Login') }
end

describe "visiting the followers page" do
before { visit followers_user_path(user) }
it { should have_selector('title', text: 'Login') }
end
end

show_follow.html.erb

<% provide(:title, @title) %>
<div class="row">
<aside class="span4">
<section>
<%= gravatar_for @user %>
<h1><%= @user.name %></h1>
<span><%= link_to "view my profile", @user %></span>
<span><b>Posts:</b> <%= @user.posts.count %></span>
</section>
<section>
<%= render 'shared/stats' %>
<% if @users.any? %>
<div class="user_avatars">
<% @users.each do |user| %>
<%= link_to gravatar_for(user, size: 30), user %>
<% end %>
</div>
<% end %>
</section>
</aside>
<div class="span8">
<h3><%= @title %></h3>
<% if @users.any? %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate %>
<% end %>
</div>
</div>

_user.html.erb

<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to 'delete', user, method: :delete,
data: { confirm: 'Are you sure?' } %>
<% end %>
</li>

我尝试从教程本身复制并粘贴所有有问题的文件,但无济于事。提到通过将 :admin 属性分配给 User 类,可以使用 admin? 方法。我猜是我将 nil 对象传递给了部分对象,但我在调试发生这种情况的确切位置时遇到了麻烦。

最佳答案

好吧,我终于明白了。

我忘记将 :following:followers 操作放在 before_filter 中,用于 :logged_in_user用户 Controller 。

class UsersController < ApplicationController
before_filter :logged_in_user,
only: [:index, :edit, :update, :destroy, :following, :followers]

这花了一段时间。

关于ruby-on-rails - Ruby on Rails 教程第 11 章 : Rspec tests fail on follower/following section,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15874039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com