【问题标题】:Rails 4: Sessions - undefined method `current_user'Rails 4:会话 - 未定义的方法“current_user”
【发布时间】:2015-05-27 06:18:27
【问题描述】:

我正在关注本教程的身份验证过程:

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

由于本教程基于 Rails 3,根据上述文章中的最后一位评论者,只需进行一些修改即可使本教程与 Rails 4 兼容。我进行了这些更改,但仍然继续追随我的尾巴问题。我已经在 Stack Overflow 上发布了另外 2 次问题,虽然我得到了“修复”手头当前问题的答案,但我担心我可能会偏离教程的从头开始的身份验证方法——进一步破坏了测试正在引起问题的多米诺骨牌效应。

关于历史,这里是按时间顺序排列的最后两个问题..

这就是我现在的失败..

  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"

..and rake 路线

    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

我很想一劳永逸地解决这个问题。这似乎是一个很棒的教程。如果我弄清楚这些更改,那么我计划将它们发布在文章的评论部分,以供其他计划使用它的人使用。

【问题讨论】:

  • 我刚刚添加了我的答案。通常从 rails 3 迁移到 rails 4 会有一些痛苦,所以你会习惯的。不过,您走在正确的轨道上。

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


【解决方案1】:

您得到undefined methodcurrent_user',因为您在profilesController 中定义了current_user,因此您使用它的视图将无法访问它。解决方案是将此方法移至 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。

【讨论】:

  • 我不敢相信我把那个滑倒了。谢谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多