Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未定义的方法'downcase';对于零:零级铁路结构_Ruby On Rails_Railstutorial.org - Fatal编程技术网

Ruby on rails 未定义的方法'downcase';对于零:零级铁路结构

Ruby on rails 未定义的方法'downcase';对于零:零级铁路结构,ruby-on-rails,railstutorial.org,Ruby On Rails,Railstutorial.org,我尝试了Railstorial rails4.0 现在是9.2.0,没有Rspec错误 但是rails s在用户中有NoMethodError#在打开/Users时索引 为什么? app/models/user.rb class User < ActiveRecord::Base before_save { self.email = email.downcase } before_create :create_remember_token validates :name, pr

我尝试了Railstorial rails4.0

现在是9.2.0,没有Rspec错误

但是
rails s
在用户中有NoMethodError#在打开/Users时索引

为什么?

app/models/user.rb

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  before_create :create_remember_token
  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, length: { minimum: 6 }

  def User.new_remember_token
    SecureRandom.urlsafe_base64
  end

  def User.encrypt(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

  private

    def create_remember_token
      self.remember_token = User.encrypt(User.new_remember_token)
    end
end
app/helpers/users\u helper.rb

module UsersHelper

  # Returns the Gravatar (http://gravatar.com/) for the given user.
  def gravatar_for(user, options = { size: 50 })
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    size = options[:size]
    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
    image_tag(gravatar_url, alt: user.name, class: "gravatar")
  end
end

您的错误似乎来自
gravatar\u for
方法的第一行,该方法调用
user.email.downcase
,表明数据库中至少有一个用户的
nil
电子邮件地址。

我没有尝试过。但也可能是这样

before_save { self.email = email.downcase if email}

标准答案是
def nil.downcase;''结束
,错误消失。可能某些用户没有电子邮件。请尝试打印所有用户记录以检查是否存在此问题。
module UsersHelper

  # Returns the Gravatar (http://gravatar.com/) for the given user.
  def gravatar_for(user, options = { size: 50 })
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    size = options[:size]
    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
    image_tag(gravatar_url, alt: user.name, class: "gravatar")
  end
end
before_save { self.email = email.downcase if email}