2
私はLDAPの認証の後にユーザーのセッションを管理しようとしています。認証部分はうまく動作しますが、セッションIDを生成できないようです。私は "真のための未定義のメソッド` id ':TrueClass "エラーを取得します。Rubyのレール - セッション管理
モデル -
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"}
ありがとうジョン。それが問題を解決しました。私の他の質問は、 - レールでは、同じユーザーIDからの複数のログインの場合、各セッションは独立したままですか? –
はい、各セッションは独立しています。 –
ありがとうございました。最後に - セッションに追加された属性を保存するにはどうしたらいいですか?電子メールアドレスやフルネームなど。 –