Devise_invitable custom email

Here is an example of a custom email that can be used with Devise Invitable:

invitable.rb

class User < ApplicationRecord
  #... other devise configurations...

  devise :invitable, :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  # Custom email settings
  invitable_email do |user, invitation|
    # Set the subject and from address
    subject "You've been invited to join our community!"
    from "[email protected]"

    # Set the body of the email
    body do
      %Q{
        Hi #{user.name},

        You've been invited to join our community by #{invitation.inviter.name}.

        To join, simply click the link below:

        <%= link_to "Join now", invitation_url(invitation.token) %>

        Best,
        #{invitation.inviter.name}
      }
    end
  end
end

In this example, we're using the invitable_email method to define a custom email template for invitations. The email template uses ERb syntax to insert dynamic values, such as the user's name and the inviter's name.

invitation.rb

class Invitation < ApplicationRecord
  #... other invitation configurations...

  # Define the invitation URL
  def invitation_url(token)
    "#{root_url}invitations/#{token}"
  end
end

In this example, we're defining a method invitation_url that generates the URL for the invitation. This URL will be used in the email template to link to the invitation.

routes.rb

Rails.application.routes.draw do
  #... other routes...

  get '/invitations/:token', to: 'invitations#show', as: 'invitation'
end

In this example, we're defining a route for the invitation URL. This route will handle the GET request for the invitation URL and render the invitation page.

invitations_controller.rb

class InvitationsController < ApplicationController
  def show
    @invitation = Invitation.find(params[:token])
    if @invitation.valid?
      # Render the invitation page
      render 'invitations/show'
    else
      # Handle invalid invitation
      render 'invitations/invalid'
    end
  end
end

In this example, we're defining a controller method show that handles the GET request for the invitation URL. The method finds the invitation by token and checks if it's valid. If it's valid, it renders the invitation page. If it's not valid, it renders an error page.

invitations/show.html.erb

<h1>Invitation</h1>

<p>You've been invited to join our community by <%= @invitation.inviter.name %>.</p>

<p>To join, simply click the link below:</p>

<%= link_to "Join now", root_url %>

In this example, we're rendering a simple invitation page that displays the inviter's name and a link to join the community.

This is just an example of how you can customize the email and invitation process with Devise Invitable. You can modify the email template, invitation URL, and invitation page to fit your specific needs.