Send multiple invitation using devise-invitable gem in rails

て烟熏妆下的殇ゞ 提交于 2020-01-25 20:36:05

问题


I want to send multiple invitations at a time using devise-invitable gem.

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| -%>
            <p><%= f.label field %><br />
            <%= f.email_field field, placeholder: "Invitation email" %></p>
        <% end -%>
        <%= f.collection_select(:role_id, Role.all, :id, :role_name, :prompt => true)  %>
    </div>
    <p>    
        <%= f.submit t("devise.invitations.new.submit_button") %>
    </p>
<% end %>

my controller:-

class Users::InvitationsController < Devise::InvitationsController
def create
    exit
    if params[:user][:email]== "" || params[:user][:role_id] == ""
        flash[:alert]="Please enter email first and Select any role for invitees"
        redirect_to new_user_invitation_path
    else
        if User.invite!(:email => params[:user][:email], :company_id => current_user.id, :type => 'Employee', :role_id => params[:user][:role_id])
            flash[:notice]="Invitation is send successfully."
            redirect_to new_user_invitation_path
        else
            flash[:alert]="Invitation is not send."
            redirect_to new_user_invitation_path
        end
    end
end
end

I think one solution is to pass comma separated emails in invite method but how can I pass it? I really don't know how.

If you have any other solution then please tell me.

Thanks.


回答1:


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.



来源:https://stackoverflow.com/questions/29099657/send-multiple-invitation-using-devise-invitable-gem-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!