未定义的局部变量或方法“current_user" [英] undefined local variable or method 'current_user'

查看:45
本文介绍了未定义的局部变量或方法“current_user"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习 RoR 教程(http://railstutorial.org/chapters/sign-in-sign-out#sec:signin_success 是相关部分),这似乎相当不错,尽管我在尝试查看示例站点时遇到了以下问题.

I'm currently going through a RoR tutorial (http://railstutorial.org/chapters/sign-in-sign-out#sec:signin_success is the relevant section) which seems to be fairly good, although I've run into the following problem when trying to view the sample site.

Extracted source (around line #10):

7:          <li><%= link_to "Home", root_path %></li>
8:          <li><%= link_to "About", about_path %></li>
9:          
10:             <% if signed_in? %>
11:                 <li><%= link_to "Profile", current_user %></li>
12:                 <li><%= link_to "Sign out", signout_path, :method => delete %></li>
13:             <% else %>

如您所见,问题出在我的方法signed_in?"上.它应该通过检查是否设置了 current_user 变量来检查用户是否已登录(我已经包含了助手中的其余代码以提供上下文,抱歉):

As you can see, the issue is stemming from my method "signed_in?" which is supposed to check if the user has logged in or not by checking whether the current_user variable is set (I've included the rest of the code from the helper to give a context, apologies):

module SessionsHelper

  def sign_in(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
    current_user = user
  end

  def sign_out
    cookies.delete[:remember_token]
    current_user = nil
  end

  def current_user= (user)
    @current_user ||= user_from_remember_token
  end

  def signed_in?
    !current_user.nil?
  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

据我所知,.nil?是一种检查对象是否已定义的方法,因此未定义的对象不应产生错误而是返回false?我在教程中搜索了 current_user 的所有案例(在检查其他人是否有这个问题但收效甚微之前),我的代码似乎是正确的,所以我有点困惑,如果有人能够帮助我理解 Ruby 变量的方式应该可以访问以及为什么我的代码不起作用,我将不胜感激.

From my understanding, .nil? is a method that checks whether or not an object has been defined and therefore the object being undefined shouldn't generate an error but rather return false? I searched the tutorial for all cases of current_user (before checking to see if anyone else had this problem with little success) and my code seems correct so I'm a little confused and if anyone is able to help me understand the way Ruby variables are supposed to be accessed and why my code isn't working I'd be most grateful.

我不确定范围是否重要,因为我刚刚开始使用 Rails 和 Ruby,但是我的用户控制器和视图正在使用帮助程序 SessionsHelper(它包含在我的应用程序控制器中)

I'm not sure if it's important with scope as I'm just beginning both Rails and Ruby, however the helper SessionsHelper is being used by my Users controller and views (it's included in my Applications controller)

推荐答案

我遇到了同样的问题 &也是出于同样的原因.您忽略了清单 9.16"中的部分说明.

I ran in to this same issue & it was for the same reason. You overlooked part of the instructions on 'Listing 9.16'.

def current_user= (user)
  @current_user ||= user_from_remember_token
end

您应该将其更改为以下内容.

You were supposed to change this to the following.

def current_user
  @current_user ||= user_from_remember_token
end

您还需要将 *self.*current_user 的所有实例更改为 *@*current_user.

You'll also want to change all of the instances of *self.*current_user to *@*current_user.

一旦你这样做,错误就会解决.

Once you do this the error(s) are resolved.

这篇关于未定义的局部变量或方法“current_user"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆