在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
追踪与放置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
I am fairly new to rails and am trying to create multiple records from an array of comma-separated emails.
I am building simple event app where the user can send 1 invitation to many emails using a single form. When the user creates an invitation a Participant record is created and , if that record is saved, an invite email is sent. I am creating the Participant record instead of using a token system so that RSVPs will be clearer.
I am having trouble getting the controller to read and separate the emails from the form.
I keep getting this:
undefined method `[]' for nil:NilClass
Here is my code:
I tried two separate Controller Methods
Try 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
Try 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
The form:
<%= 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' %>
Models
class Event < ActiveRecord::Base has_many :participants end class Participant < ActiveRecord::Base belongs_to :event end
trace with 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
原文:https://stackoverflow.com/questions/27773069
最满意答案
所以最好在没有它的情况下尝试相关链接。
![Alt text](doc/images/pullNewChangesFromMaster.png "About to Pull down the new changes")
Reading the Guidance page from Microsoft, I do not see
?raw=true
mentioned anywhere.So it is best to try your relative link without it.
![Alt text](doc/images/pullNewChangesFromMaster.png "About to Pull down the new changes")
相关问答
更多-
在降价时显示降价(Display markdown in markdown)[2024-01-04]
将其渲染为代码。 对于内联代码,请使用反引号,例如 使用单个星号表示斜体文字: *italic* 使用双星号表示粗体文字: **bold** 上一个列表中的Markdown片段分别写为`*italic*`和`**bold**` 。 对于代码块 ,将整个块缩进四个空格: # Title * One * Two * Three 许多实现还允许您使用三个反引号来代替压缩代码块 ,但这并不是完全标准化的。 Render it as code. For inline code us ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a