Rails 教程中 Ruby 中 RelationshipsController 的未定义方法 'logged_in_user'
Undefined method 'logged_in_user' for RelationshipsController in Ruby on Rails Tutorial
这有点奇怪。我正在浏览 rails 教程中 ruby 的第十二章(供参考:https://www.railstutorial.org/book/following_users),并到达第 12.2.4 节,所有测试都通过了。然后我执行命令 rails generate controller Relationships
并准确输入清单 12.30 和 12.31 中的代码以尝试让关系控制器测试通过,但我收到以下错误:
ERROR["test_destroy_should_require_logged-in_user", RelationshipsControllerTest, 2015-11-13 11:07:25 +0000]
test_destroy_should_require_logged-in_user#RelationshipsControllerTest (1447412845.16s)
NoMethodError: NoMethodError: undefined method `logged_in_user' for #<RelationshipsController:0x000000044f5bd8>
test/controllers/relationships_controller_test.rb:14:in `block (2 levels) in <class:RelationshipsControllerTest>'
test/controllers/relationships_controller_test.rb:13:in `block in <class:RelationshipsControllerTest>'
test/controllers/relationships_controller_test.rb:14:in `block (2 levels) in <class:RelationshipsControllerTest>'
test/controllers/relationships_controller_test.rb:13:in `block in <class:RelationshipsControllerTest>'
ERROR["test_create_should_require_logged-in_user", RelationshipsControllerTest, 2015-11-13 11:07:25 +0000]
test_create_should_require_logged-in_user#RelationshipsControllerTest (1447412845.22s)
NoMethodError: NoMethodError: undefined method `logged_in_user' for #<RelationshipsController:0x00000004db9990>
test/controllers/relationships_controller_test.rb:7:in `block (2 levels) in <class:RelationshipsControllerTest>'
test/controllers/relationships_controller_test.rb:6:in `block in <class:RelationshipsControllerTest>'
test/controllers/relationships_controller_test.rb:7:in `block (2 levels) in <class:RelationshipsControllerTest>'
test/controllers/relationships_controller_test.rb:6:in `block in <class:RelationshipsControllerTest>'
为什么它看不到来自用户控制器的 logged_in_user 方法?我可以把它写成
class RelationshipsController < ApplicationController
before_action :logged_in_user
def create
user = User.find(params[:followed_id])
current_user.follow(user)
redirect_to user
end
def destroy
user = Relationship.find(params[:id]).followed
current_user.unfollow(user)
redirect_to user
end
private
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
end
并且测试通过了,但是这种行为违反了在整个教程中反复强调的“不要重复自己”原则。知道出了什么问题吗?
那个方法 logged_in_user
是在 UsersController
中还是在 ApplicationsController
中?如果它在 UsersController
中,由于层次继承,它将无法访问它。尝试将该方法放入 ApplicationsController
,然后旋转它。
这样做将使 UsersController
和 RelationshipsController
都可以访问此方法,因为它们都继承自 ApplicationsController
这有点奇怪。我正在浏览 rails 教程中 ruby 的第十二章(供参考:https://www.railstutorial.org/book/following_users),并到达第 12.2.4 节,所有测试都通过了。然后我执行命令 rails generate controller Relationships
并准确输入清单 12.30 和 12.31 中的代码以尝试让关系控制器测试通过,但我收到以下错误:
ERROR["test_destroy_should_require_logged-in_user", RelationshipsControllerTest, 2015-11-13 11:07:25 +0000]
test_destroy_should_require_logged-in_user#RelationshipsControllerTest (1447412845.16s)
NoMethodError: NoMethodError: undefined method `logged_in_user' for #<RelationshipsController:0x000000044f5bd8>
test/controllers/relationships_controller_test.rb:14:in `block (2 levels) in <class:RelationshipsControllerTest>'
test/controllers/relationships_controller_test.rb:13:in `block in <class:RelationshipsControllerTest>'
test/controllers/relationships_controller_test.rb:14:in `block (2 levels) in <class:RelationshipsControllerTest>'
test/controllers/relationships_controller_test.rb:13:in `block in <class:RelationshipsControllerTest>'
ERROR["test_create_should_require_logged-in_user", RelationshipsControllerTest, 2015-11-13 11:07:25 +0000]
test_create_should_require_logged-in_user#RelationshipsControllerTest (1447412845.22s)
NoMethodError: NoMethodError: undefined method `logged_in_user' for #<RelationshipsController:0x00000004db9990>
test/controllers/relationships_controller_test.rb:7:in `block (2 levels) in <class:RelationshipsControllerTest>'
test/controllers/relationships_controller_test.rb:6:in `block in <class:RelationshipsControllerTest>'
test/controllers/relationships_controller_test.rb:7:in `block (2 levels) in <class:RelationshipsControllerTest>'
test/controllers/relationships_controller_test.rb:6:in `block in <class:RelationshipsControllerTest>'
为什么它看不到来自用户控制器的 logged_in_user 方法?我可以把它写成
class RelationshipsController < ApplicationController
before_action :logged_in_user
def create
user = User.find(params[:followed_id])
current_user.follow(user)
redirect_to user
end
def destroy
user = Relationship.find(params[:id]).followed
current_user.unfollow(user)
redirect_to user
end
private
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
end
并且测试通过了,但是这种行为违反了在整个教程中反复强调的“不要重复自己”原则。知道出了什么问题吗?
那个方法 logged_in_user
是在 UsersController
中还是在 ApplicationsController
中?如果它在 UsersController
中,由于层次继承,它将无法访问它。尝试将该方法放入 ApplicationsController
,然后旋转它。
这样做将使 UsersController
和 RelationshipsController
都可以访问此方法,因为它们都继承自 ApplicationsController