简体   繁体   中英

Undefined method 'logged_in_user' for RelationshipsController in Ruby on Rails Tutorial

This is kind of bizarre. I'm walking through chapter twelve of the ruby on rails tutorial (for reference: https://www.railstutorial.org/book/following_users ) and got to section 12.2.4 with all tests passing. I then did the command rails generate controller Relationships and entered exactly the code in listings 12.30 and 12.31 to try and get the relationships controller test to pass, but I'm getting the following errors:

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>'

Why isn't it seeing the logged_in_user method from the users controller? I can put it in as

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

and the test passes, but this kind of violates the Don't Repeat Yourself principle harped on throughout the tutorial. Any ideas what's going wrong?

Is that method logged_in_user on the in the UsersController or in the ApplicationsController ? If it's in the UsersController it will not have access to it because of the hierarchal inheritance. Try putting that method into the ApplicationsController and then giving it whirl.

Doing this will give both the UsersController and the RelationshipsController access to this method because they both inherit from ApplicationsController

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM