Rails 4:会话 - 未定义的方法“current_user”
Rails 4: Sessions - undefined method `current_user'
我正在遵循本教程的身份验证过程:
http://larsgebhardt.de/user-authentication-with-ruby-on-rails-rspec-and-capybara/
由于本教程基于 Rails 3,根据上面文章中最后一位评论者的说法,只需进行少量修改即可使本教程与 Rails 4 兼容。我做了这些变化,但仍然继续追逐我的问题。我在 Stack Overflow 上还有 2 次 posted 问题,虽然我得到的答案是 'fix' 当前手头的问题,但恐怕我可能会偏离教程的身份验证方法scratch - 进一步破坏导致问题多米诺骨牌效应的测试。
为了了解一点历史,这里是按时间顺序排列的最后两个问题..
所以这就是我现在的失败..
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[=21类似=]
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
这是带有注销 link 代码的 _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"
..和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
我很想一劳永逸地解决这个问题。这似乎是一个很棒的教程。如果我弄清楚这些变化,那么我计划在文章的评论部分为任何其他计划使用它的人 post。
您得到 undefined method
current_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 中。
我正在遵循本教程的身份验证过程:
http://larsgebhardt.de/user-authentication-with-ruby-on-rails-rspec-and-capybara/
由于本教程基于 Rails 3,根据上面文章中最后一位评论者的说法,只需进行少量修改即可使本教程与 Rails 4 兼容。我做了这些变化,但仍然继续追逐我的问题。我在 Stack Overflow 上还有 2 次 posted 问题,虽然我得到的答案是 'fix' 当前手头的问题,但恐怕我可能会偏离教程的身份验证方法scratch - 进一步破坏导致问题多米诺骨牌效应的测试。
为了了解一点历史,这里是按时间顺序排列的最后两个问题..
所以这就是我现在的失败..
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[=21类似=]
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
这是带有注销 link 代码的 _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"
..和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
我很想一劳永逸地解决这个问题。这似乎是一个很棒的教程。如果我弄清楚这些变化,那么我计划在文章的评论部分为任何其他计划使用它的人 post。
您得到 undefined method
current_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 中。