我正在尝试在使用 ldap 进行身份验证后管理用户会话。身份验证部分运行良好,但似乎无法生成会话 ID。我收到“未定义的方法 `id' for true:TrueClass”错误。
模型 -
def self.Authenticate(login, pass)
user = find_by_user_id(login)
if user
user
else
return false
end
conn = Net::LDAP.new(
:host => SERVER,
:port => PORT,
:base => BASE,
:auth => {
:username => "#{login}@#{DOMAIN}",
:password => pass,
:method => :simple
}
)
if conn.bind
return true
else
return false
end
rescue Net::LDAP::LdapError => e
return false
end
控制器 -
def create
user = User.Authenticate(params[:user_id], params[:password])
if user
session[:user_id] = user.id
redirect_to theapp_url, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
看法 -
<%= form_tag sessions_path do %>
<p>
<%= label_tag :Username %><br />
<%= text_field_tag :user_id, params[:user_id] %>
</p>
<p>
<%= label_tag :Password %><br />
<%= password_field_tag :password %>
</p>
<p class="button"><%= submit_tag "Log in" %></p>
<% end %>
错误 -
NoMethodError in SessionsController#create
undefined method `id' for true:TrueClass
Rails.root: /myapp
Application Trace | Framework Trace | Full Trace
app/controllers/sessions_controller.rb:12:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"OmzCrLHR1t/xfIXNcEzy2NCGfVpEKSyI4OZfqpPEFNw=",
"user_id"=>"admin",
"password"=>"[FILTERED]",
"commit"=>"Log in"}