wik*_*hen 1 ruby rspec ruby-on-rails ruby-on-rails-3 railstutorial.org
我正在研究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'
Run Code Online (Sandbox Code Playgroud)
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)>'
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
_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>
Run Code Online (Sandbox Code Playgroud)
我尝试从教程本身复制并粘贴所有有问题的文件无济于事.有人提到,通过将:admin属性赋值给User类,admin?可以使用一种方法.我猜它是以某种方式我将一个nil对象传递给部分,但我无法调试究竟发生了什么.
好的,我终于明白了.
我忘了在用户控制器中将:following和:followers操作放在before_filterfor :logged_in_user中.
class UsersController < ApplicationController
before_filter :logged_in_user,
only: [:index, :edit, :update, :destroy, :following, :followers]
Run Code Online (Sandbox Code Playgroud)
这需要一段时间.
| 归档时间: |
|
| 查看次数: |
698 次 |
| 最近记录: |