从 rails 中以逗号分隔的电子邮件列表创建多条记录

create multiple records from comma separated email list in rails

我是 rails 的新手,我正在尝试从一组以逗号分隔的电子邮件中创建多条记录。

我正在构建简单的事件应用程序,用户可以使用一个表单向多封电子邮件发送 1 份邀请。当用户创建邀请时,会创建参与者记录,如果保存了该记录,则会发送邀请电子邮件。我正在创建参与者记录而不是使用令牌系统,这样 RSVP 会更清晰。

我无法让控制器读取电子邮件并将其与表单分开。

我一直收到这个:

undefined method `[]' for nil:NilClass

这是我的代码:

我尝试了两种不同的控制器方法

尝试 1

class ParticipantsController < ApplicationController
    def participant_invite
           @event = Event.find(params[:e])
           @email = params[:participant_invite][:email].split(/,\s*/)
           @participant = Participant.create!(event_id: @event.id, email: email_list,  level: 4, participant_cat_id: 3, added_by: current_user.id, status: 'unseen')
           respond_to do |format|
            if @participant.save
              format.html { redirect_to @participant, notice: 'Participant was successfully created.' }
              format.json { render :show, status: :created, location: @participant }
            else
              format.html { render :new }
              format.json { render json: @participant.errors, status: :unprocessable_entity }
            end
          end
        end

尝试 2:

def participant_invite
    @participant = params[:participant_invite][:email].split(/,\s*/)
    @participant.each do |p|
      newparticipant = Participant.new(:email => p)
      newparticipant.save
    end
    redirect_to tags_path
  end

表格:

<%= form_for :participant_invite, url: add_participant_path( :e => @event.id) do |f| %> 

<%=    f.email_field :email, :autofocus => true, :required => true, :maxlength => 55, :placeholder => 'Email(s)', :class => 'form-control'  %>

<%= f.submit 'Invite' %>

模特

class Event < ActiveRecord::Base

    has_many :participants

end

class Participant < ActiveRecord::Base

    belongs_to :event

end

跟踪 puts params.inspect

Started GET "/participants/participant_invite/276/1/1" for 127.0.0.1 at 2015-01-04 19:52:24 -0800
Processing by ParticipantsController#participant_invite as HTML
  Parameters: {"e"=>"276"}
  [1m[36mUser Load (0.7ms)[0m  [1mSELECT  "users".* FROM "users"  WHERE "users"."id" = 2  ORDER BY "users"."id" ASC LIMIT 1[0m

Completed 500 Internal Server Error in 57ms

NoMethodError (undefined method `[]' for nil:NilClass):
  app/controllers/participants_controller.rb:91:in `participant_invite'


  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.5ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.9ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.8ms)
  Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (12.1ms)
source=rack-timeout id=f896977ded104ababd4b01919c155066 timeout=10000ms service=461ms state=completed

所以,我想通了,这是我的解决方案

控制器

def participant_invite
 @event = Event.find(params[:e])
end  

def add_participants
  @event = Event.find(params[:e]) 
  @participant = params[:participant_invite][:email].split(/,\s*/)
  @participant.each do |t|
  newpart = Participant.new(:event_id => @event.id, :status => 'unseen')
  newpart.save
    EventMailer.notify_participant_new(newpart, @event,  @participant_invite ).deliver
  end
  redirect_to invited_participants_path(:e => @event.id )
end

查看

 <%= form_for :participant_invite, url: add_participants_path(:e => @event.id) } do |f| %> 

 <%=    f.text_field :email, :placeholder => 'Email(s)'  %>

 <%= f.submit %>