现在我尝试创建一个登录表单但是当我运行我的代码时,它会抛出:[can't cast Hash to string]
我该如何解决?
tbluser Model
class Tbluser < ActiveRecord::Base
self.primary_key = "username"
def self.login(user,pass)
if user and pass
find(:conditions => ['username LIKE ? and password LIKE ?',"\#{user.to_s}","\#{pass.to_s}"])
else
find(:all)
end
end
end
登录控制器
class LoginController < ApplicationController
protect_from_forgery with: :null_session
def formlogin
end
def chekclogin
@dn = Tbluser.login(params[:user],params[:pass])
if @dn.blank?
redirect_to:action => "error"
else
redirect_to:action => "success"
end
end
def souc
end
def error
end
end
登录表单视图
<form class="login-form" accept-charset="UTF-8" action="/chekclogin" method="get">
<%= text_field_tag :user, '', class: 'form-control', :placeholder => 'Username'%>
<%= password_field_tag(:pass, "", placeholder: "Password")%>
<button>login</button>
<p class="message">Not registered? <a href="#">Create an account</a></p>
</form>
答案 0 :(得分:1)
您可以在这里做几件事来检查发生了什么:
/logs/development.logs
中的日志,您可以看到传递给控制器的参数。Rails.logger.info(params)
来查看参数是什么。如果您仍然卡住,那么如果您可以粘贴development.log
的请求日志,我可以进一步查看并提供帮助。
作为旁注,这是在rails 5中基本登录的一个很好的教程:https://www.railstutorial.org/book/basic_login
祝你好运!