NoMethodError in UsersController#show
undefined method `signed_in?' for #<UsersController:0x5bf3980>
Rails.root: C:/test_app
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:91:in `signed_in_user'
Request
Parameters:
{"id"=>"1"}
Show session dump
Show env dump
Response
Headers:
None
在用户控制器
def signed_in_user
redirect_to signin_path, notice: "Please Sign In." unless signed_in?
end
在 SessionsController 中
def signed_in?
!current_user.nil?
end
module SessionsHelper
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
end
def sign_out
cookies.delete(:remember_token)
self.current_user = nil
end
def current_user=(user)
@current_user = user
end
def current_user
@current_user ||= user_from_remember_token
end
def signed_in?
!current_user.nil?
end
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign In"
render 'new'
else
sign_in user
flash.now[:error] = "Welcome, #{user.name}"
render 'AdCon'
end
end
def destroy
sign_out
redirect_to root_path
end
private
def user_from_remember_token
User.authenticate_with_salt(*remember_token)
end
def remember_token
cookies.signed[:remember_token] || [nil,nil]
end
end
*编辑: ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** **
我正在使用以下教程: http ://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users @ Listing 9.12
清单 9.12。在过滤器之前添加一个 signed_in_user。
应用程序/控制器/users_controller.rb
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:edit, :update]
.
.
.
private
def signed_in_user
redirect_to signin_path, notice: "Please sign in." unless signed_in?
end
end
当我在 SessionsController 中包含助手时,我收到了消息
未定义的方法`signed_in?' for # 提取的源代码(在第 9 行附近):
<div>
<% if controller.signed_in? %> <----LINE 9
<%= link_to "Sign Out", signout_path, :method => :delete %>
<% else %>
<%= link_to "Sign IN" , signin_path %>
我像这样包含了 Helper:class SessionsController < ApplicationController include SessionsHelper
我无法让它工作,所以我将辅助方法复制到 SessionsController 并且错误消失了。现在我遇到了 9.12 where signed_in 的问题?是一种未知的方法。这是有道理的,因为 signed_in? 通过助手在 SessionsController 中。UserController 可以访问该功能。我是rails新手,很困惑。
感谢所有的反馈
编辑:* ** * ** * ** * ** * ** * **
这是应用程序控制器
class ApplicationController < ActionController::Base
protect_from_forgery
include ActionView::Helpers::SessionsHelper
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
end