Rails 7中Supervisor登录后重定向出错引发500错误求助
问题:Supervisor身份登录失败,会话已创建但无法完成登录
问题描述
尽管已创建会话,但无法以Supervisor身份完成登录。
控制台日志
Started GET "/supervisor_session/new" for 127.0.0.1 at 2023-12-12 22:03:32 +0200 Processing by SupervisorSessionController#new as TURBO_STREAM Rendering layout layouts/application.html.erb Rendering supervisor_session/new.html.erb within layouts/application Rendered supervisor_session/new.html.erb within layouts/application (Duration: 4.3ms | Allocations: 1988) Rendered layout layouts/application.html.erb (Duration: 7.4ms | Allocations: 4361) Completed 200 OK in 22ms (Views: 8.8ms | ActiveRecord: 0.7ms | Allocations: 11337) Started POST "/supervisor_session" for 127.0.0.1 at 2023-12-12 22:03:37 +0200 Processing by SupervisorSessionController#create as TURBO_STREAM Parameters: {"authenticity_token"=>"[FILTERED]", "supervisor"=>{"email"=>"root@root.root", "password"=>"[FILTERED]"}, "commit"=>"Login"} Supervisor Load (0.2ms) SELECT "supervisors".* FROM "supervisors" WHERE "supervisors"."email" = ? LIMIT ? [["email", "root@root.root"], ["LIMIT", 1]] ↳ app/controllers/supervisor_session_controller.rb:7:in `create' #<Supervisor id: 2, first_name: "root", last_name: "root", password_digest: [FILTERED], password_salt: [FILTERED], email: "root@root.root", phone_number: "00">Redirected to http://localhost:3000/administrator_session/main_menu/new?allow_other_host=true ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LOOK HERE Completed 302 Found in 337ms (ActiveRecord: 0.2ms | Allocations: 2839) Started GET "/administrator_session/main_menu/new?allow_other_host=true" for 127.0.0.1 at 2023-12-12 22:03:38 +0200 Processing by MainMenuController#new as TURBO_STREAM Parameters: {"allow_other_host"=>"true"} Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms | Allocations: 734) ArgumentError (wrong number of arguments (given 0, expected 2)): app/helpers/logged_in_user_helper.rb:8:in `LoggedInUserConstructor' app/controllers/main_menu_controller.rb:4:in `new'
SupervisorSessionController代码
class SupervisorSessionController < ApplicationController def new @supervisor = Supervisor.new end def create @supervisor = Supervisor.find_by(email: params[:supervisor][:email]) print @supervisor.inspect if @supervisor && @supervisor.authenticate(params[:supervisor][:password]) session[:supervisor_id] = @supervisor.id session[:first_name] = @supervisor.first_name session[:last_name] = @supervisor.last_name respond_to do |format| format.html {redirect_to controller: :main_menu, action: :new , allow_other_host: true} end else flash[:alert] = "Login failed" render "new" end end def destroy session[:supervisor_id] = nil redirect_to '/', flash: { error: "Please Retry" } end private # Only allow a list of trusted parameters through. def supervisor_session__params params.require(:supervisor_session).permit(:email,:password_digest) end end
错误分析与解决方案
从日志可见,Supervisor身份验证已通过(成功查询到用户并完成校验,执行了重定向),但重定向到MainMenuController#new时触发参数错误:LoggedInUserConstructor方法要求2个参数,调用时未传入任何参数。
解决步骤:
- 检查
MainMenuController#new方法第4行,确认调用LoggedInUserConstructor时是否遗漏参数。 - 打开
app/helpers/logged_in_user_helper.rb查看第8行的方法定义,明确所需参数类型(如用户ID、用户类型)。 - 在
MainMenuController#new中从session读取supervisor_id,并传入用户类型(如:supervisor),示例:def new LoggedInUserConstructor(session[:supervisor_id], :supervisor) # 其他业务逻辑 end - 移除重定向时的
allow_other_host: true参数(本地环境无跨域需求,避免冗余参数干扰):format.html { redirect_to controller: :main_menu, action: :new } - 在
MainMenuController中先校验session[:supervisor_id]是否存在,确保用户已登录后再执行后续逻辑。
内容的提问来源于stack exchange,提问作者user199135
相关产品推荐
相关产品推荐

