Why does this unused self.hash method cause a “can't convert String into Integer” error?

好久不见. 提交于 2019-12-30 13:37:34

问题


I am running through the Lynda Rails 3 tutorial. At one point, in a controller called access_controller, we call a method from a model called AdminUser. The original call was:

authorized_user = AdminUser.authenticate(params[:username], params[:password])

When I run rails server, open up the browser, and access the appropriate view, I get the error: TypeError, can't convert String into Integer

This same question has been asked twice before. The first time, the asker says the problem resolved itself the next day. (I first ran into this 3 days ago, so that has not happened.) The second question has not been answered. I will try to provide much more detail:

The method in the model was:

  def self.authenticate(username="", password="")
    user = AdminUser.find_by_username(username)
    if user && user.password_match?(password)
      return user
    else
      return false
    end
  end

When I call this method from the rails console, it works totally fine. Something about calling it from a controller, or trying to get at via the browser, seems to be going wrong (I am relative beginner, so I apologize that I cannot express this thought better). I have since replicated this error with a more simple method in the same AdminUser model:

  def self.nothing
    true
  end

This still gives me the same error. I then tried calling the self.nothing method from a different controller and action (called pages_controller#show). When I tried to open that up in the browser, I once again got the same error: "can't convert String into Integer"

I then created an identical self.nothing method in my Subject model. When I try to run that method from the show action in pages_controller, it works totally fine. No errors.

So, the same method runs totally fine in rails console, totally fine when I place it in my Subject model, but produces an error when I place it in my AdminUser model.

I then tried to comment out basically everything in sight in my AdminUser model to see if I can make the error go away. I finally was able to. The error was apparently caused by another method:

 def self.hash(password="")
   Digest::SHA1.hexdigest(password)
 end

I was supposed to have deleted this method a few video lessons ago when we added these other methods:

 def self.make_salt(username="")
   Digest::SHA1.hexdigest("Use #{username} with #{Time.now} to make salt")
 end

 def self.hash_with_salt(password="", salt="")
   Digest::SHA1.hexdigest("Put #{salt} on the #{password}")
 end

I never deleted the initial one, but for some reason, it was the one causing the error.

So, my question now is: Why did leaving in that method (which was not being used anywhere) cause this "can't convert String into Integer" error?


回答1:


The reason is that User.hash overrides Object.hash that should return a Fixnum. You should change it's name for something like User.make_hash



来源:https://stackoverflow.com/questions/9267484/why-does-this-unused-self-hash-method-cause-a-cant-convert-string-into-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!