我正在研究Hartl教程的Chapter 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)>'
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
对象传递给部分内容,但我无法调试到底发生了什么。
答案 0 :(得分:5)
好的,我终于明白了。
我忘了将:following
和:followers
操作放在用户控制器中before_filter
的{{1}}。
:logged_in_user
这需要一段时间。
答案 1 :(得分:2)
您的代码正在检查current_user.admin?
并根据该代码更改显示的内容。但是,根据您的规范标题,您似乎正在测试未登录用户访问该页面时的行为:current_user将为nil
听起来_user partial中的if语句需要一个额外的条件来检查是否有登录用户,然后才检查该用户是否为admin
答案 2 :(得分:1)
尝试查看users_controller
,你的部分代码看起来对我来说没问题,这意味着它可能是幕后的东西。本地服务器跟踪似乎指向第68行和关注者方法。我看起来像这样,fyi:
def followers
@title = "Followers"
@user = User.find(params[:id])
@users = @user.followers.paginate(page: params[:page])
render 'show_follow'
end
希望有所帮助。
答案 3 :(得分:1)
我也忘了在before_filter中使用:follow和:followers操作:用户控制器中的:logged_in_user,但我很好奇这个错误。
就像Frederick Cheung指出的那样,在parcial的代码中存在错误:_user.html.erb。我们应该在调用admin之前检查current_user是否为nil?方法就可以了。
所以而不是:
<% 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>
我们通过在before_filter中输入:follow和:followers操作来删除最后一个:logged_in_user。