【发布时间】:2019-01-17 03:47:36
【问题描述】:
我正在构建一个注册应用程序。共有三种型号:Participant、Volunteer_Detail 和Student_Detail。 Participant has_one 的其他两个模型,而它们依次为 belongs_to Participant。我正在尝试使用一种带有 fields_for 的表单向Participant 和其他模型之一提供数据。
我不断收到错误消息,说用于Student_Detail 的属性之一不是Participant 的已定义属性。
我查看了fields_for 上的几个指南,似乎有几种方法可以定义您的表单所针对的第二个模型。
共识似乎是它应该是这样的:
<%= f.fields_for @participant.student_details do |student_detail_field| %>
<%= f.label :nationality, 'Nationality:' %>
<%= student_detail_field.text_field :nationality %>
但是,它似乎只在我写 :student_details 而不是@participant.student_details 时显示我的表格。
这是我的表单,显示了顶部和包含 fields_for 的部分(修剪表单以节省空间):
<%= f.label :first_name, 'First:' %>
<%= f.text_field :first_name %>
<%= f.fields_for :student_details do |student_detail_field| %>
<%= f.label :nationality, 'Nationality:' %>
<%= student_detail_field.text_field :nationality %>
这是我的参与者控制器:
类 ParticipantsController
def new2
@participant= Participant.new
end
def create
@participant = Participant.create(participant_params)
@participant.save
if @participant.save
flash[:success] = "Successfully Registered!"
#email notifes admin of new registration
NotifyMailer.notify_email(@participant).deliver_later
redirect_to '/signup'
end
end
def edit
end
def update
end
def show
end
def index
end
private
def participant_params
params.require(:participant).permit(:first_name, :last_name, :gender, :email, :birthdate, :phone, :street_name, :city, :state, :zip, :nationality, :religion, :need_ride,
:has_spouse, :spouse_name, :english_level, :expectations, :length_of_stay, :exact_length, :volunteer_id, :matched, :returned_home, :participant_id, :participant_id_id)
end
end
这是服务器错误:
NoMethodError (undefined method `nationality' for #<Participant:0x000000000d039410>
Did you mean? Rational):
app/controllers/participants_controller.rb:11:in `create'
Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.9ms)
Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.0ms)
Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (790.6ms)
看看是否有人可以帮助澄清我的错误。
谢谢!
【问题讨论】:
标签: ruby-on-rails activeadmin belongs-to nomethoderror has-one