首页 文章

使用设备进行用户注册/登录会根据用户的响应将用户重定向到不同的表单页面

提问于
浏览
0

我正在使用设计用户注册/登录,用户成功注册后,我想显示一个页面/一个对话框,并根据用户响应重定向到另一个页面 . 我怎样才能做到这一点?

User Model (By devise)

  • 用户名

  • 密码

Student Model

  • 名字

  • student_id

Teacher Model

  • 名字

  • 年级

First_page:注册登录链接注册链接将显示设计视图/ devise / registrations / new.html.erb页面 . 成功注册后,用户将获取根页面 . 我在routes.rb中定义了根页面:

`Rails.application.routes.draw做devise_for:用户资源:学生,:老师

#有关此文件中可用DSL的详细信息,请参阅http://guides.rubyonrails.org/routing.html root to:"students#index" end`

此时,应用程序不知道用户是谁 .

所以,我想从用户那里获得身份信息(学生/老师) .

我如何获得这些信息?

学生/教师控制器:`class StudentsController <ApplicationController before_action:authenticate_user!,only:[:new,:create] def index @students = Student.all end

def new
  @student = Student.new
end

def create
  current_user.create_student(student_params)
  redirect_to root_path
end

private
def student_params
  params.require(:student).permit(:name, :skypid)
end

end`

用户成功登录后,我想询问用户是学生还是教师 . 根据他们选择的内容,将其重定向到学生表单页面或教师表单页面 .

我怎么能在铁轨上做到这一点?

谢谢

1 回答

  • 0

    您可以在 ApplicationController 中编写自定义 after_sign_in_path_for 函数,假设您正在使用所有默认的Devise控制器 . 它返回的任何命名路径助手或其他路径都将是用户重定向的位置,因此您可以执行一些简单的操作,例如始终重定向到选择页面,该页面显示选项并处理后续操作的选择:

    def after_sign_in_path_for(resource)
      user_type_selection_path # whatever route in your app manages the selection
    end
    

    或者,您可以在该函数中的用户模型上调用自定义方法,以便在那里做出选择:

    def after_sign_in_path_for(resource)
      resource.student? ? student_path : teacher_path
    end
    

    当然,当选择已经完成并且重定向时,你可以混合这些以及后者,使用类似于以下内容的东西:

    def after_sign_in_path_for(resource)
      if resource.user_type_chosen?
        resource.student? ? student_path : teacher_path
      else
        user_type_selection_path
    end
    

    请记住,这些功能或路径都不是真实的,因为我可以提供,但希望这会让你朝着正确的方向前进 . after_sign_in_path_for 钩子是你的主要工具,除非你进入覆盖默认设计控制器的世界并中断通常的工作流程以适应这一步骤,这似乎不是你的描述所必需的 .

相关问题