在rails中使用devise-invitable gem发送多个邀请 [英] Send multiple invitation using devise-invitable gem in rails

查看:242
本文介绍了在rails中使用devise-invitable gem发送多个邀请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 devise-invitable gem发送多个邀请。



new.html。 erb

 < h2> 
<%= tdevise.invitations.new.header%>
< / h2>
<%= form_for资源,as => resource_name,:url => invitation_path(resource_name),:html => {:method => :post} do | f | %GT;
<%= devise_error_messages! %GT;
< div>
<%resource.class.invite_key_fields.each do | field | - %GT;
< p><%= f.label字段%>< br />
<%= f.email_field字段,占位符:邀请电子邮件%>< / 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%>

我的控制器: -

  class Users :: InvitationsController< devise :: InvitationsController 
def create
exit
如果params [:user] [:email] ==|| params [:user] [:role_id] ==
flash [:alert] =请先输入电子邮件,然后选择任何被邀请人的角色
redirect_to new_user_invitation_path
else
如果User.invite!(:email => params [:user] [:email],:company_id => current_user.id,:type =>'Employee',:role_id => params [:user] :role_id])
flash [:notice] =邀请成功发送。
redirect_to new_user_invitation_path
else
flash [:alert] =邀请不发送。
redirect_to new_user_invitation_path
end
end
end
end

我认为一个解决方案是在邀请方法中传递逗号分隔的电子邮件,但是如何通过?我真的不知道如何。



如果您有任何其他解决方案,请告诉我。



谢谢

解决方案

我遇到这个问题,但最终我有解决方案可以一次发送多个邀请函。



下面我解释我的代码,我如何成为可能这个。



这是我的html视图。



new.html.erb

 < h2> 
<%= tdevise.invitations.new.header%>
< / h2>

<%= form_for资源,:as => resource_name,:url => invitation_path(resource_name),:html => {:method => :post} do | f | %GT;
<%= devise_error_messages! %GT;
< div>
<%resource.class.invite_key_fields.each do | field | - %GT;
<%= f.label字段%>< br />
<%= f.email_field字段,名称:user [email] [],占位符:邀请电子邮件,必需:true%>< / p>

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

在此我将默认采用一个文本框,名称为user [email] [] (这很重要,因为使用此rails会自动创建电子邮件数组,并在您提交表单时发送参数)



我还使用JavaScript生成动态文本框,它将创建在div下方点击邀请更多链接按钮:

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

这是JavaScript的动态文本框代码。


$ b append('< input type =emailid =email'+ i +'name =user [email] [$] $('#createNewTextbox' ]占位符=邀请电子邮件需要/>');

现在你可以看到我给动态文本框(name =user [email ] []),所以rails会自动创建这样的哈希数组:

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

现在这个哈希数组传递给create方法,在这个方法中,我们从params中获取每个电子邮件,并将其发送给邀请方法发送邀请。



我的控制器: -

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

这样做...



如果还有任何查询,然后告诉我。


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.

解决方案

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.

这篇关于在rails中使用devise-invitable gem发送多个邀请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆