【发布时间】:2015-01-05 03:35:48
【问题描述】:
我对 Rails 很陌生,正在尝试从一系列以逗号分隔的电子邮件创建多条记录。
我正在构建简单的活动应用程序,用户可以使用一个表单向多封电子邮件发送 1 个邀请。当用户创建邀请时,会创建参与者记录,如果保存了该记录,则会发送邀请电子邮件。我正在创建参与者记录而不是使用令牌系统,以便回复更清晰。
我无法让控制器读取电子邮件并将其与表单分开。
我不断收到这个:
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
【问题讨论】:
-
很可能你没有得到
params[:participant_invite]的值...它是零...然后当你尝试调用[:email]时它会爆炸。 -
您使用的是什么版本的 Rails?您的控制器中有要求/许可吗?
-
无关注意:您不需要在每个变量名前加上
@。如果变量要保留并显示在视图中,您只需要@......同样 - 对代表一组事物的事物使用复数变量名称是一种很好的做法......例如,而不是@participant的我会使用participants -
我的下一个大问题是:如果你正在做
form_for- 你为什么要传递不同的网址?因为我认为您正在使用的 url 只接受您传递给自身的内容并忽略文件的其余部分...您可以检查此 url 的路由吗?是 POST 还是 GET? -
这就是 form_for 应该如何使用:apidock.com/rails/ActionView/Helpers/FormHelper/form_for 如果你想做一个不是标准 RESTful 路由的 url(就像你一样),那么使用 form-tag:apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag(你还必须将
f.email_field转换为email_field_tag或类似内容以匹配请参阅:apidock.com/rails/v4.1.8/ActionView/Helpers/FormTagHelper/…)
标签: ruby-on-rails forms ruby-on-rails-4 input