简体   繁体   English

Rails:如何让设计中的两个用户模型通过注册

[英]Rails: how to let two user models in devise go through registration

In my app there are two models: 在我的应用中,有两种模型:

  • Users 用户数
  • Teachers 教师

Registering Teachers is a problem because my registration controller is set up for user registration only (I think), so I keep getting errors about the params missing "user". 注册老师是一个问题,因为我的注册控制器设置为仅用于用户注册(我认为),因此我不断收到关于缺少“用户”的参数的错误。

Registrations_controller.rb Registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController

  private

  def sign_up_params
    params.require(:user).permit(:teacher, :teacher_id, :user_id, :username, :first_name, :last_name, :email, :password, :password_confirmation)
  end

  def account_update_params
    params.require(:user).permit(:teacher, :teacher_id, :user_id, :username, :first_name, :last_name, :email, :password, :password_confirmation, :current_password)
  end
end

So my questions are: 所以我的问题是:

  • Do you know a way to tweak the registrations controller to handle both registrations? 您知道一种调整注册控制器以处理两个注册的方法吗?
  • Should I even be doing it like this or ought it be handled differently? 我应该这样做还是应该以不同的方式处理?

Just in case, here is the registration form that is being used. 以防万一,这里是正在使用的注册表。 Thanks for your time. 谢谢你的时间。

devise>registrations>new.html.erb devise>注册> new.html.erb

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
<div>
  <%= f.input :username, required: true, autofocus: true %>
    <%= f.input :email, required: true %>

<% if request.fullpath.include?('user') %>
       <%= f.input :teacher_id, required: false %>
<% end %>


    <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
    <%= f.input :password_confirmation, required: false %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

In your config/routes.rb 在你的config/routes.rb

devise_for :users, controllers: { registrations: "registrations" }
devise_for :teachers, controllers: { registrations: "teacher/registrations" }

Create a controller app/controllers/teacher/registrations_controller.rb 创建一个控制器app/controllers/teacher/registrations_controller.rb

class Teacher::RegistrationsController < Devise::RegistrationsController

  private

  def sign_up_params
    params.require(:teacher).permit(:username, :first_name, :last_name, :email, :password, :password_confirmation)
  end

  def account_update_params
    params.require(:teacher).permit(:username, :first_name, :last_name, :email, :password, :password_confirmation, :current_password)
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM