我正在建设一个数字图书馆,我已经完成了很多需要的功能。我目前在将数字图书馆与学习管理系统 (LMS) 集成时遇到问题。
我已经有一个数字图书馆的管理员身份验证系统,使用设计宝石。我的目标是允许想要访问数字图书馆的用户使用其学习管理系统 (LMS) 凭据(用户名和密码)登录数字图书馆。
我已获得登录 API 端点和其他所需的参数学习管理系统(LMS),我已经创建了用户模型, the 会话控制器和会话视图模板.
我目前正在使用RestClient 宝石对于API调用,我只想在API调用成功后将登录信息保存到session[:user_id] = user.id
,但我有一个错误#SessionsController 的未定义局部变量或方法“user”。我想不出哪里出了问题。
下面是我的源代码
会话控制器
require 'rest-client'
class SessionsController < ApplicationController
def new
end
def create
response = RestClient::Request.execute(
method: :post,
url: 'https://newapi.example.com/token',
payload: { 'username': params[:username],
'password': params[:password],
'grant_type':'password' },
headers: { apiCode: '93de0db8-333b-4f478-aa92-2b43cdb7aa9f' }
)
case response.code
when 400
flash.now[:alert] = 'Email or password is invalid'
render 'new'
when 200
session[:user_id] = user.id
redirect_to root_url, notice: 'Logged in!'
else
raise "Invalid response #{response.to_str} received."
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, notice: 'Logged out!'
end
end
会话新视图
<p id=”alert”><%= alert %></p>
<h1>Login</h1>
<%= form_tag sessions_path do %>
<div class="field">
<%= label_tag :username %>
<%= text_field_tag :username %>
</div>
<div class="field">
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<div class="actions">
<%= submit_tag 'Login' %>
</div>
<% end %>
用户模型
class User < ApplicationRecord
has_secure_password
validates :username, presence: true, uniqueness: true
end
对于代码示例的任何形式的帮助将不胜感激。如果需要,我也愿意提供有关此集成的更多信息。先感谢您。