为什么在Ruby on Rails上出现nil:NilClass的错误未定义方法`name'的错误? [英] Why am I getting error Undefined method `name' for nil:NilClass with Ruby on Rails?

查看:124
本文介绍了为什么在Ruby on Rails上出现nil:NilClass的错误未定义方法`name'的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为Rails中默认使用诸如姓名和电子邮件之类的方法吗?

I thought methods such as name and email were default in rails?

在我的静态页面视图中,在profile.html.erb中,我有:

In my static pages view, in profile.html.erb I have:

<% if logged_in? %>
<% provide(:title, @user.name) %>
<% else %>
<% provide(:title, 'Profile')%>
<% end %>

我放入了static_page_controller

I put in my static_page_controller

def profile
  @user = User.find_by_remember_token(:remember_token)
end

当我进入控制台时,User.find_by_remember_token("actualtoken").name向我返回适当的用户名,但:remember_token却没有.如何使:remember_token =登录的用户记住令牌?

When I go to the console User.find_by_remember_token("actualtoken").name returns me the appropriate users name, but :remember_token does not. How do I make :remember_token = the logged in users remember token?

在我的session_helper中,

In my sessions_helper I have

def log_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    current_user = user
  end

  def logged_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
      @current_user ||= user_from_remember_token
  end

  def log_out
    current_user = nil
    cookies.delete(:remember_token)
  end


    private

    def user_from_remember_token
      remember_token = cookies[:remember_token]
      User.find_by_remember_token(remember_token) unless remember_token.nil?
    end
end

将其复制到我的static_pages_helper中并没有完成任何操作.

copying it to my static_pages_helper didn't accomplish anything.

推荐答案

关于Rails框架和ruby语言的快速了解:

Quick things you should be aware of the rails framework and the ruby language:

  • 在您的任何帮助器中定义的功能将对所有帮助器和视图可用(因此,没有理由通过不同的帮助器复制和粘贴相同的功能);
  • 您可能正在使用身份验证gem,但我猜它是Devise gem.如果这是正确的,那么除非您有理由这样做,否则您不应覆盖他们的助手;
  • User.anything将从User类中调用静态函数任何内容
  • user = User.find_by_anything(the_thing)是ActiveModel提供的类静态帮助器,它将查询数据库以查找具有* anything = the_thing *的用户;该用户或nil将被返回;
  • user.an_attribute将调用一个函数,该函数返回用户指定的属性(默认情况下与该属性的列名相同);
  • user.try(:anything)将尝试从用户处调用该函数,并返回其值.如果用户为nil,则返回的值也将为nil.
  • A function defined in any of your helpers will be available to all helpers and views (so there is no reason to copy and paste the same functions through different helpers);
  • You're probably using an authentication gem and I guess it is the Devise gem. If this is right, then you should not be overriding their helpers unless you have a reason to do this;
  • User.anything will call the static function anything from the User class;
  • user = User.find_by_anything(the_thing) is a class static helper provided by ActiveModel that will query the database looking for a user that has *anything = the_thing*; this user or nil will be returned;
  • user.an_attribute will call a function that returns the user specified attribute (which is the same as the column name of this attribute by default);
  • user.try(:anything) will try to call the function anything from the user and return its value. If user is nil, the returned value will also be nil.

也就是说,我想您只是想检索当前的用户记住令牌,可以通过以下操作来实现:

That said, I guess you just wanted to retrieve the current user remember token, which can be accomplished with the following:

user = current_user.try(:remember_token)

已编辑:该问题有些混乱,但我还认为以下代码可与您的控制器配合使用:

EDITED: The question is a bit messy, but I also think the following code will work with your controller:

def profile
  @user = User.find_by_remember_token(params[:remember_token])
end

您必须通过params哈希访问请求的参数.

You must access the request's parameters through the params hash.

这篇关于为什么在Ruby on Rails上出现nil:NilClass的错误未定义方法`name'的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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