1

我正在编写Hartl 教程的第 11 章,试图non-logged-in users in the Users controller visiting the followers and following page通过它。我遇到了一个错误,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)>'

授权页面规范.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对象传递给了部分,但我在调试发生这种情况的地方时遇到了麻烦。

4

4 回答 4

5

好吧,我终于想通了。

我忘记在用户控制器的for中添加:following:followers操作。before_filter:logged_in_user

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

那花了一段时间。

于 2013-04-10T21:18:26.940 回答
2

您的代码正在检查current_user.admin?和更改基于此显示的内容。但是,从您的规范标题看来,您正在测试未登录用户访问该页面时的行为: current_user 将为 nil

听起来 _user 部分中的 if 语句需要一个额外的条件来检查是否有登录用户,然后再检查该用户是否为管理员

于 2013-04-08T08:57:00.253 回答
1

尝试查看users_controller,您的部分代码对我来说看起来不错,这意味着它可能是幕后缺少的东西。本地服务器跟踪似乎指向第 68 行和 follower 方法。我的看起来像这样,仅供参考:

def followers 
   @title = "Followers"
   @user = User.find(params[:id])
   @users = @user.followers.paginate(page: params[:page])
   render 'show_follow'
end 

希望有帮助。

于 2013-04-08T09:12:10.037 回答
1

我也忘记将 :following 和 :followers 操作放在 User 控制器中的 :logged_in_user 的 before_filter 中,但我很好奇这个错误。

就像 Frederick Cheung 指出的那样,在 parcial 的代码中存在错误:_user.html.erb。我们应该在调用管理员之前检查 current_user 是否为零?方法就可以了。

所以而不是:

 <% if current_user.admin? && !current_user?(user) %>

你应该有:

 <% if current_user && current_user.admin? && !current_user?(user) %>

这将删除“未定义的方法管理员?” 错误并给我们正确的测试错误:

 Expected response to be a <redirect>, but was <200>

我们通过将 :following 和 :followers 操作放在 :logged_in_user 的 before_filter 中来删除最后一个。

于 2014-12-29T17:34:57.540 回答