【发布时间】:2016-07-05 21:42:59
【问题描述】:
我正在学习使用 Rails 4 & Devise 构建社交网络的课程。
我一直在试图通过一门课程,这是我遇到的错误:
1) Error:
UserFriendshipsControllerTest#test_: #new when not logged in should get redirected to the login page. :
NoMethodError: undefined method `authenticate!' for nil:NilClass
test/controllers/user_friendships_controller_test.rb:8:in `block (3 levels) in <class:UserFriendshipsControllerTest>'
这是我的 users_friendships_controller_test.rb :
require 'test_helper'
class UserFriendshipsControllerTest < ActionController::TestCase
context "#new" do
context "when not logged in" do
should "get redirected to the login page" do
get :new
assert_response :redirect
end
end
end
end
我的 user_friendship.rb:
class UserFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: "User", foreign_key: "friend_id"
private
def friendship_params
params.require(:user).permit(:user_id, :friend_id, :friend)
end
end
我添加到 user_friendships_controller.rb 中的唯一内容是一个空白的新方法,并且:
before_filter :authenticate_user!, only: [:new]
如果我应该发布更多代码,请告诉我。
谢谢
【问题讨论】:
-
你能提供
user_friendships_controller列表 -
@farhatmihalko
class UserFriendshipsController < ApplicationControllerbefore_filter :authenticate_user!, only: [:new]def newendend
标签: ruby-on-rails ruby devise shoulda