![](/img/trans.png)
[英]Authlogic and Rails 4 - “current_user is undefined method”
[英]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上发表了两次其他问题,尽管我得到的答案可以“解决”当前问题,但恐怕我可能会从头开始偏离本教程的身份验证方法-进一步破坏了测试引起问题的多米诺骨牌效应。
有关历史,这里是按时间顺序排列的最后两个问题。
所以这就是我现在的失败。
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
我很想一劳永逸。 它似乎是一个很棒的教程。 如果我发现了更改,那么我打算将它们发布在文章的评论部分中,以供其他计划使用的人使用。
您将获得undefined method
current_user undefined method
,因为您是在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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.