Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了无法在ruby / ruby​​-on-rails中将String转换为整数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想验证用户是否存在,如果存在,请执行密码匹配.

我的控制器看起来像这样

def attempt_login
    authorized_user = User.authenticate(params[:username],params[:password])
    if authorized_user
        flash[:notice] = "successfully logged in."
        redirect_to(:action => 'menu')
    else
        flash[:notice] = "Invalid username/password"
        redirect_to(:action => 'login')
    end
end

该模型看起来像:

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

def password_match?(password="")
    hashed_password == User.hash_with_salt(password,salt)
end

在执行此操作的过程中,我收到TypeError(无法将String转换为Integer).我怀疑当我想为authorized_user设置一个值时发生错误,但不知道如何处理这个问题.

编辑:
我的服务器日志为空.但我可以发布框架跟踪:

activesupport (3.0.8) Lib/active_support/descendants_tracker.rb:23:in `delete'
activesupport (3.0.8) Lib/active_support/descendants_tracker.rb:23:in `block in clear'
activesupport (3.0.8) Lib/active_support/descendants_tracker.rb:21:in `each'
activesupport (3.0.8) Lib/active_support/descendants_tracker.rb:21:in `clear'
railties (3.0.8) Lib/rails/application/bootstrap.rb:59:in `block (2 levels) in <module:Bootstrap>'
activesupport (3.0.8) Lib/active_support/callBACks.rb:420:in `_run_call_callBACks'
actionpack (3.0.8) Lib/action_dispatch/middleware/callBACks.rb:44:in `call'
rack (1.2.3) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.8) Lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.8) Lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.8) Lib/rails/rack/logger.rb:13:in `call'
rack (1.2.3) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.8) Lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.3) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.3) lib/rack/lock.rb:11:in `call'
actionpack (3.0.8) Lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.8) Lib/rails/application.rb:168:in `call'
railties (3.0.8) Lib/rails/application.rb:77:in `method_missing'
railties (3.0.8) Lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.3) lib/rack/content_length.rb:13:in `call'
rack (1.2.3) lib/rack/handler/webrick.rb:52:in `service'
E:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
E:/Ruby192/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
E:/Ruby192/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

这是develop.log,如果它可以帮助:

Started POST "/access/attempt_login" for 127.0.0.1 at 2011-06-27 20:19:19 +0200
DEPRECATION WARNING: config.action_view.debug_rjs will be removed in 3.1,from 3.1 onWARDs you will need to install prototype-rails to conTinue to use RJS templates . (called from <top (required)> at G:/Projects/basicsocial/app/controllers/application_controller.rb:1)
Processing by AccessController#attempt_login as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"jvGVYjeypK9sWoaaa5c2OwzBKmCMX7h7Wp28vBH9wfw=","username"=>"test88","password"=>"[FILTERED]","commit"=>"Log In"}
<-[1m<-[36mSQL (7.0ms)<-[0m  <-[1mdescribe `users_pages`<-[0m
<-[1m<-[35mSQL (2.0ms)<-[0m  SHOW TABLES
<-[1m<-[36mUser Load (0.0ms)<-[0m  <-[1mSELECT `users`.* FROM `users` WHERE `users`.`username` = 'test88' LIMIT 1<-[0m
Redirected to http://localhost:3000/admin
Completed 302 Found in 545ms

TypeError (can't convert String into Integer):
Rendered E:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered E:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (5.0ms)
Rendered E:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.0ms)

salt变量是我用来更好地保护用户页面的方法.这是存储在数据库中的东西.

解决方法

显然代码没有任何问题.它可能与我的浏览器缓存或某种性质有关,因为它在第二天就开始工作了.抱歉给你带来不便.

大佬总结

以上是大佬教程为你收集整理的无法在ruby / ruby​​-on-rails中将String转换为整数全部内容,希望文章能够帮你解决无法在ruby / ruby​​-on-rails中将String转换为整数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。