简体   繁体   中英

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

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

<%= 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

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

Create a controller 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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