How to Send Custom Invitation Using Devise_Invitable

Customize link of invitation sent by devise_invitable rails

You can customize mailer view with your own stuff

Use this for generate devise invitable views rails generate devise_invitable:views
For more information about view check this Link

And customize app/views/devise/mailer/invitation_instruction.html.erb to get your stuff working.

UPDATE

When you invite user it will call create action of Users::InvitationsController and you can overide the controller also, refere Customize devise invitable Controller

Send multiple invitation using devise-invitable gem in rails

i trouble with this problem but finally i got solution to send multiple invitation email at a time.

below i explain my code that how i become possible this.

here is my html view.

new.html.erb

<h2>
<%= t "devise.invitations.new.header" %>
</h2>

<%= form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :post} do |f| %>
<%= devise_error_messages! %>
<div>
<% resource.class.invite_key_fields.each do |field| -%>
<%= f.label field %><br />
<%= f.email_field field, name: "user[email][]", placeholder: "Invitation email", required: true %></p>

<div id="createNewTextbox" >
</div>
<a id="btnLinkCreate" href="#" onClick="create_new();">
+ INVITE MORE
</a>
<% end -%>
</div>
<p>
<%= f.submit t("devise.invitations.new.submit_button") %>
</p>
<% end %>

in this i take one text-box by-default with name : "user[email][]" (it is important because using this rails automatically create email array and send in params whene you submit form )

i also generate dynamic text-box using JavaScript and it will create below div when click on invite more link button :

<div id="createNewTextbox" >
</div>
<a id="btnLinkCreate" href="#" onClick="create_new();">
+ INVITE MORE
</a>

here is my dynamic text-box code of JavaScript.

$('#createNewTextbox').append('<input type="email" id="email'+i+'" name="user[email][]" placeholder="Invitation email" required/>');

now you can see that i give same name to dynamic text-box (name="user[email][]"), so rails automatically create hash array like this:

"user" => { "email" => { "email-1","email-2","email-3"... } } 

now this hash array is pass in create method in which we fetch every email from params and give it to invite method to send the invitation.

my controller :-

class Users::InvitationsController < Devise::InvitationsController
def create
params[:user][:email].each do |email|
User.invite!(:email => email)
end
end
end

thats it...

if still you have any query then tell comment me.

Use devise_invitable to send invitation upon creating new model record

Alright, figured it out (or at least figured one way out). Basically I created a set of dummy variables in the Company model like so:

attr_accessor :user_email, :user_fname, :user_lname

Then I updated the controller as follows

def new
@company=Company.new
@folder=@company.folders.build
@stock=@company.stocks.build
@stock.security_class="Common"
@stock.security_series=""
end

def create
@company = Company.new(company_params)
if @company.save
invitedUser=User.invite!(email: company_params[:user_email], fname: company_params[:user_fname], lname: company_params[:user_lname], invited_by: current_user)
if invitedUser.save
UserCompany.create(user: invitedUser, company: @company, company_role: "Owner")
redirect_to companies_path, notice: "User successfuly created!"
end
else
redirect_to welcome_index_path
end
end

The key highlights were

  1. to remove @user=@company.user_companies.build.build_user because I don't want to build the user, I want it to be created by the invite! method.
  2. To use User.invite! to create the user and grab the necessary data out of the submitted form's parameters
  3. To manually add a join record (because I have a many-to-many relation between Company and User).

That's it really.

Devise invitable custom message not working - rails

Might be this will work.

 User.invite!({email: email, message: params[:user][:custom_message] }, current_user)

If you are getting Un permitted error, you should do something like in either application or User controller.

before_filter :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
devise_parameter_sanitizer.for(:invite).concat [:custom_message]
end

You also have to edit invitation mailer, since you are using custom_message attribute which has no value . You should use message since you are assigning data into it. So

<%= @resource.custom_message %>

changes to

<%= @resource.message %>


Related Topics



Leave a reply



Submit