Rails 4:会话 - 未定义的方法`current_user'

时间:2015-03-24 02:42:46

标签: ruby-on-rails session authentication rspec capybara

我正在遵循本教程的身份验证过程:

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

由于本教程基于Rails 3,根据上面文章中的最后一个评论者,只需要进行一些修改就可以使教程与Rails 4兼容。我做了这些修改但仍然继续追逐我的尾巴的问题。我已经在Stack Overflow上发布了其他两个问题,虽然我得到的答案是“修复”#目前的问题,我担心我可能会从头开始偏离教程的认证方法 - 进一步打破导致问题多米诺骨牌效应的测试。

对于一些历史,这是按时间顺序排列的最后两个问题。

所以现在我失败了......

  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

该方法存在于我的 profiles_controller.rb 中 - 类似于教程 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

这是带有“注销”链接代码的 _header.html.erb 文件。

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

我的 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"

..和佣金路线

    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

我很乐意一劳永逸地解决这个问题。这似乎是一个很棒的教程。如果我弄清楚了这些变化,那么我计划将其发布在文章的评论部分,供计划使用它的任何人使用。

1 个答案:

答案 0 :(得分:2)

您正在获得undefined method current_user&#39;因为您在current_user中定义了profilesController,所以它不会被您使用它的视图访问。解决方案是将此方法移动到applicationController,如下所示:

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

现在您可以在任何视图文件夹中使用current_user。我刚刚检查了教程,它告诉你将这个方法添加到applicationController,所以你可能错误地将它添加到profilesController。