设计邀请批量邀请 - Ruby on Rails

发布于 2024-11-30 08:24:45 字数 139 浏览 0 评论 0原文

我正在尝试使用 Devise invitable 来一次添加多个用户。基本上,当有人创建帐户时,他们要做的第一件事就是使用可邀请表单添加一堆用户......

只是不确定如何复制表单中的字段并让他们为每个条目发送创建请求。

提前致谢!

I'm trying to user Devise invitable to add multiple users at once. Basically when someone creates an account, the first thing they'll want to do is add a bunch of users using the invitable form...

just not sure how to duplicate fields in the form and have them send a create request for each entry.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此刻的回忆 2024-12-07 08:24:45

我就是这样做的。

提供一个接受以逗号分隔的电子邮件列表的文本区域。定义
向此列表中的每封电子邮件发送邀请的新操作。

对于此示例,我们假设有一个用户模型和一个用户控制器。

定义batch_invite操作的路由。

resources :users do
  collection do
    post 'batch_invite'
  end
end

在users_controller中定义batch_invite操作

def batch_invite
  #Validate the user_emails field isn't blank and emails are valid
  params[:user_emails].split(",").each do |email|
    User.invite!(:email => email)
  end
  #redirect_to appropriate path
end

在文本区域中接受以逗号分隔的电子邮件列表的表单。 >

<%= form_tag batch_invite_users_path, :method => :post do %>
  <%= label_tag "Email ids of people you'd like to invite." %>
  <%= text_area_tag :user_emails %>
  <%= submit_tag "Invite!" %>
<% end %>

一些注意事项:

  1. 如果您喜欢精简控制器,您可以将逻辑移至模型,例如,通过在用户模型中创建 send_batch_invitations 方法并传递params[:user_emails] 作为来自 users 控制器的该方法的参数。

  2. 由于发送邀请的方法可能需要足够的时间才能完成,我建议您将此任务分配给后台作业处理器,例如 delayed_jobresque
    railscasts 演示了这两个后台作业处理器的用法。

This is how I would do it.

Provide a text area that accepts a comma-separated email list. Define
a new action that sends an invitation to each email in this list.

Lets assume a User model and a users controller for this example.

Define a route for the batch_invite action.

resources :users do
  collection do
    post 'batch_invite'
  end
end

Define the batch_invite action in the users_controller

def batch_invite
  #Validate the user_emails field isn't blank and emails are valid
  params[:user_emails].split(",").each do |email|
    User.invite!(:email => email)
  end
  #redirect_to appropriate path
end

A form that accepts a comma-separated list of emails in a textarea.

<%= form_tag batch_invite_users_path, :method => :post do %>
  <%= label_tag "Email ids of people you'd like to invite." %>
  <%= text_area_tag :user_emails %>
  <%= submit_tag "Invite!" %>
<% end %>

A couple of notes :

  1. If you like your controller skinny, you could move the logic to the model, for instance, by creating a send_batch_invitations method in your User model and pass the params[:user_emails] as an argument to that method from the users controller.

  2. Since the method that sends the invitations could take sufficient time to complete, I would suggest you assign this task to a background job processor, such as delayed_job or resque.
    There are railscasts that demonstrate the usage of these two background job processors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文