更改 Devise_invitable attend_key_fields 以发送邀请

问题描述 投票:0回答:1

所以,我目前正在开发一个应用程序,我需要允许学校的管理员和老师向新用户发送邀请。

到目前为止一切顺利,我使用了 Devise_invitable 并且邀请令牌的发送工作正常。问题是当我尝试向邀请表单添加其他字段并将它们写入数据库时。我正在尝试添加名字、姓氏和角色,但唯一保存的是我写的电子邮件。

注意:我还尝试将学生存储在与用户表不同的表中。

我尝试遵循我在这里看到的一些相当旧的线程,并遵循 devise_invitable 文档。我的代码目前如下:

用户::邀请控制器

class Users::InvitationsController < Devise::InvitationsController
  before_action :configure_permitted_parameters

  def create
    super do |resource|
      if resource.persisted? && resource.role == "aluno"
        AlunoProfile.create(
          set_params
        )
      end
    end
  end

  private

  def set_params
    params.require(:user).permit(
      :first_name,
      :last_name,
      :morada,
      :birthdate,
      :nif,
      :numero_utente,
      :cartao_cidadao,
      :validity,
      :contacto,
      :encarregado_educação,
      :grau_parentesco,
      :contacto_emergencia,
      :aulas,
      :horario_preferencial,
      :recolha_dados,
      :fotografias_captadas
    )
  end

  protected

  # Permit the new params here.
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:invite, keys: [:first_name, :last_name, :role])
  end
end

用户模型

class User < ApplicationRecord
  has_many :user_aulas
  has_many :aulas, through: :user_aulas
  has_many :bookings
  has_one :aluno_profile
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :invitable, :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, invite_for: 2.weeks

  enum role: [:aluno, :professor, :admin]

  after_initialize :set_default_role, :if => :new_record?

  def set_default_role
    self.role ||= :user
  end

  def full_name
    "#{first_name} #{last_name}"
  end
end

学生档案模型

class AlunoProfile < ApplicationRecord
  belongs_to :user
end

邀请查看

<h2 class="mx-3 mt-2"><%= t "devise.invitations.new.header" %></h2>

<%= simple_form_for(resource, as: resource_name, url: invitation_path(resource_name), html: { method: :post }) do |f| %>
  <%= f.error_notification %>

  <%= resource.class.invite_key_fields.each do |field| -%>
    <div class="form-inputs mx-3">
      <%= f.input :first_name, input_html: {placeholder: "John"} %>
      <%= f.input :last_name, input_html: {placeholder: "Doe"} %>
      <%= f.input field, input_html: {placeholder: "[email protected]"} %>
      <%= f.input :role, as: :select, collection: [:aluno, :professor], prompt: "Selecionar uma opção", input_html: {class: "form-select"} %>
    </div>
  <% end -%>

  <div class="form-actions mx-3">
    <%= f.button :submit, t("devise.invitations.new.submit_button"), class: 'btn btn-primary' %>
  </div>
<% end %>
ruby-on-rails ruby rubygems devise devise-invitable
1个回答
0
投票

由于

invite_key_fields
是 devise_invitable gem 中的类方法,因此在您的
User
模型中,您可以覆盖此类方法(假设first_name 和 last_name 属性在此模型中):

def self.invite_key_fields
  %i[email first_name last_name]
end
© www.soinside.com 2019 - 2024. All rights reserved.