How do i add custom fields to Devise-invitable

86 Views Asked by At

I have generated the view with rails generate devise_invitable:views I have also edited the generated views in users/invitations/new.html.erb to include the fields I want to include first name and last name


<%= form_for(resource, as: resource_name, url: invitation_path(resource_name), html: { method: :post }) do |f| %>
  <%= render "users/shared/error_messages", resource: resource %>

  <% resource.class.invite_key_fields.each do |field| -%>
    <div class="field">
      <%= f.label field %><br />
      <%= f.text_field field %>
    </div>
    <div class="field">
      <%= f.label field %><br />
      <%= f.text_field :fist_name %>
    </div>
    <div class="field">
      <%= f.label field %><br />
      <%= f.text_field :last_name %>
    </div>
  <% end -%>

  <div class="actions">
    <%= f.submit t("devise.invitations.new.submit_button") %>
  </div>
<% end %>

I have added this line of code # devise_for :users, controllers: { invitations: 'users/invitations' } to the routes.rb file

I alse created a controller to inherit the Devise controller

class Users::InvitationsController < Devise::InvitationsController
    def update
      if some_condition
        redirect_to root_path
      else
        super
      end
    end

    def create
        
    end

    def new
        
    end
  end

yet still, when I go to the new_user_invitation_path to make an invite I get only the email field. please how do I make custom fields?

1

There are 1 best solutions below

0
RodneyH On

Assuming you've added the first_name and last_name fields to your user's table.

Permitting Custom Fields:

In your application_controller.rb, permit custom fields when accepting an invitation.

def configure_permitted_parameters
  devise_parameter_sanitizer.permit :accept_invitation, keys: [:first_name, :last_name, :email]
end

This step ensures that the custom fields :first_name and :last_name are permitted when a user accepts an invitation.

Invitations Controller:

In your invitations_controller.rb add a before_action hook to call configure_permitted_parameters.

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

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:invite, keys: [:first_name, :last_name])
  end
end

In the controller, we are permitting the custom fields :first_name and :last_name when inviting a user. This ensures that the values entered in the form are accepted when sending an invitation.

Invitation Instructions Email Template:

In the invitation_instructions.html.erb template include the first name and last name of the invited user.

<p>
  <strong><%= t("devise.mailer.invitation_instructions.first_name") %>:</strong> <%= @resource.first_name %> 
  <strong><%= t("devise.mailer.invitation_instructions.last_name") %>:</strong> <%= @resource.last_name %>
</p>

--> Skip this if you already have some User Mailer

User Model: Define an after_create callback named eg send_admin_mail. This callback method is automatically triggered after a new user is created.

class User < ApplicationRecord
  after_create :send_admin_mail

  def send_admin_mail
    SomeMailer.welcome_email(self).deliver_later
  end
end

Within the send_admin_mail method, after sending you call UserMailer.welcome_email(self).deliver to send a welcome email to the newly registered user. Here, self refers to the instance of the user that triggered the after_create callback.

By invoking UserMailer.welcome_email(self), you're passing the user instance to the welcome_email method in the UserMailer class, which then uses the user's information to construct and send a personalized welcome email.

User Mailer: Assuming you have some Mailer working located in app/mailers/some_mailer.rb that defines an email template for sending emails to users. Make sure you are using ApplicationMailer as the base class for your mailer, as shown in this example:

class SomeMailer < ApplicationMailer
  def welcome_email(user)
    @user = user
    @url  = "https://sitename.com/"

    subject = "Welcome, #{@user.first_name} #{@user.last_name}"
    mail(to: @user.email, subject: subject)
  end
end

--> Skip End

To test sending an invitation to a user, use the invite! class method.

rails c

User.invite!(email: '[email protected]', name: 'John Doe')
# => an invitation email will be sent to [email protected]

If you have the letter_opener_web gem installed and setup, after sending the email the template will open in your browser interface automatically.

If you run into issues with the above check the Devise Invitable.