shroy
shroy

Reputation: 918

Rails 4: Sessions - undefined method `current_user'

I am following along this tutorial's authentication process:

http://larsgebhardt.de/user-authentication-with-ruby-on-rails-rspec-and-capybara/

Since the tutorial is based off Rails 3, according to the last commenter in the article above, there are only a few modifications needed to make the tutorial compatible with Rails 4. I made those changes but still continue to chase my tail with issues. I have posted questions 2 other times on Stack Overflow and although I'm getting answers that 'fix' the current issue at hand, I'm afraid I might be straying away from the tutorial's approach of authentication from scratch - further breaking the tests which is causing a domino effect of issues.

For a bit of history, here is the last two questions in chronological order..

So here's the failure I have now..

  5) User Management User log out
     Failure/Error: activate(@writer)
     ActionView::Template::Error:
       undefined method `current_user' for #<SessionsController:0x007fd2029c8d68>

users_spec.rb..

feature 'User Management' do

    background do
      @writer = create(:user, :writer)
    end

scenario 'User log out' do
  activate(@writer)
  login(@writer)
  logout(@writer)
  expect(page).to have_content "Successfully logged out."
end

The method exists in my profiles_controller.rb - which is similar to the tutorial's offices_controller.rb

class ProfilesController < ApplicationController


  def show
    auth_required
    access_only_with_roles("writer", "admin")
  end

  def current_user
    @current_user ||=  User.find(session[:user_id]) if session[:user_id]
  end
end

This is the _header.html.erb file with the Log Out link code.

    <a href="#"><%= link_to "Sign Up", new_user_path %></a>
    <a href="#"><%= link_to "Log In", new_session_path %></a>
    <a href="#"><%= link_to "Log Out", session_path(current_user), method: :delete %></a>

My routes.rb..

Rails.application.routes.draw do
  get 'profiles/show'

  get 'sessions/new'

  get 'users/new'

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
 root 'sessions#new'

  resources :posts do
    resources :comments
end


  resources :sessions
  resources :users
  resources :profile

  get "activate/:code" => "users#activate", :as => "activate"

..and rake routes

    Prefix Verb   URI Pattern                                 Controller#Action
    profiles_show GET    /profiles/show(.:format)                    profiles#show
     sessions_new GET    /sessions/new(.:format)                     sessions#new
        users_new GET    /users/new(.:format)                        users#new
             root GET    /                                           sessions#new
    post_comments GET    /posts/:post_id/comments(.:format)          comments#index
                  POST   /posts/:post_id/comments(.:format)          comments#create
 new_post_comment GET    /posts/:post_id/comments/new(.:format)      comments#new
edit_post_comment GET    /posts/:post_id/comments/:id/edit(.:format) comments#edit
     post_comment GET    /posts/:post_id/comments/:id(.:format)      comments#show
                  PATCH  /posts/:post_id/comments/:id(.:format)      comments#update
                  PUT    /posts/:post_id/comments/:id(.:format)      comments#update
                  DELETE /posts/:post_id/comments/:id(.:format)      comments#destroy
            posts GET    /posts(.:format)                            posts#index
                  POST   /posts(.:format)                            posts#create
         new_post GET    /posts/new(.:format)                        posts#new
        edit_post GET    /posts/:id/edit(.:format)                   posts#edit
             post GET    /posts/:id(.:format)                        posts#show
                  PATCH  /posts/:id(.:format)                        posts#update
                  PUT    /posts/:id(.:format)                        posts#update
                  DELETE /posts/:id(.:format)                        posts#destroy
         sessions GET    /sessions(.:format)                         sessions#index
                  POST   /sessions(.:format)                         sessions#create
      new_session GET    /sessions/new(.:format)                     sessions#new
     edit_session GET    /sessions/:id/edit(.:format)                sessions#edit
          session GET    /sessions/:id(.:format)                     sessions#show
                  PATCH  /sessions/:id(.:format)                     sessions#update
                  PUT    /sessions/:id(.:format)                     sessions#update
                  DELETE /sessions/:id(.:format)                     sessions#destroy
            users GET    /users(.:format)                            users#index
                  POST   /users(.:format)                            users#create
         new_user GET    /users/new(.:format)                        users#new
        edit_user GET    /users/:id/edit(.:format)                   users#edit
             user GET    /users/:id(.:format)                        users#show
                  PATCH  /users/:id(.:format)                        users#update
                  PUT    /users/:id(.:format)                        users#update
                  DELETE /users/:id(.:format)                        users#destroy
    profile_index GET    /profile(.:format)                          profile#index
                  POST   /profile(.:format)                          profile#create
      new_profile GET    /profile/new(.:format)                      profile#new
     edit_profile GET    /profile/:id/edit(.:format)                 profile#edit
          profile GET    /profile/:id(.:format)                      profile#show
                  PATCH  /profile/:id(.:format)                      profile#update
                  PUT    /profile/:id(.:format)                      profile#update
                  DELETE /profile/:id(.:format)                      profile#destroy
         activate GET    /activate/:code(.:format)                   users#activate

I would love to figure this out once and for all. It appeared to be a great tutorial. If I figure out the changes then I plan to post them in the comment section of the article for anyone else who plans to use it.

Upvotes: 1

Views: 1701

Answers (1)

Ahmad Al-kheat
Ahmad Al-kheat

Reputation: 1795

You are getting undefined methodcurrent_user' because you defined current_user in the profilesController,` so it won't be accessible to the view in which you are using it. The solution is to move this method to the applicationController like this :

class ApplicationController < ActionController::Base

  protect_from_forgery

  helper_method :current_user

  private
  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
end

So now you can use current_user in any view folder. I just checked the tutorial and it tells you to add this method to applicationController, so you probably added it to profilesController by mistake.

Upvotes: 2

Related Questions