我正在编写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
对象传递给了部分,但我在调试发生这种情况的地方时遇到了麻烦。