这个问题已经在这里有一个答案:
我有使用fields_for
两个模型提交数据的表单。主要的模式被称为Participant
,和第二种模式是Student_Detail
。 Participant
的has_one
Student_Detail
。
当试图加载在开发模式的形式,我得到几个不同的NoMethodErrors,所有的fields_for
Student_Detail下摔倒属性。如果我从表单中删除一个属性,下一个成为NoMethodError。
我终于得到了我的控制器设置来处理两种模式一种形式。
我试着与被列为NoMethodError的特定属性来缩小是否其中一人造成的问题消除表单字段,但似乎在Student_Detail
所有属性的这种形式是造成此错误。
这里是我的表格:
<%= form_for @participant, url: {action: "create"} do |f| %>
<%= f.label :first_name, 'First:' %>
<%= f.text_field :first_name %>
<%= f.label :last_name, 'Last:' %>
<%= f.text_field :last_name %>
<%= f.label :gender, 'Sex:' %>
<%= f.select :gender, ['Male', 'Female'] %>
<br>
<br>
<%= f.label :birthdate, 'Birthdate:' %>
<%= f.date_field :birthdate %>
<%= f.label :email, 'Email:' %>
<%= f.text_field :email %>
<%= f.label :phone, 'Phone:' %>
<%= f.text_field :phone %>
<%= f.label :street_name, 'Street Number & Name:' %>
<%= f.text_field :street_name %>
<%= f.label :city, 'City:' %>
<%= f.text_field :city %>
<%= f.label :state, 'State:' %>
<%= f.text_field :state %>
<%= f.label :zip, 'Zip:' %>
<%= f.text_field :zip %>
<%= f.hidden_field :role, :value => 'Reader' %>
<%= f.fields_for @participant.student_detail do |student_detail_field| %>
<%= f.label :nationality, 'Nationality:' %>
<%= student_detail_field.text_field :nationality %>
<%= f.label :religion, 'Religion:' %>
<%= student_detail_field.text_field :religion %>
<%= f.label :need_ride, 'Do you need help getting to the building?' %>
<%= student_detail_field.select :need_ride, ['Yes','No'] %>
<br>
<br>
<%= f.label :has_spouse, 'Marital Status:' %>
<%= student_detail_field.select :has_spouse, ['married', 'single'] %>
<br>
<br>
<%= f.label :spouse_name, "If married, spouse's name:" %>
<%= student_detail_field.text_field :spouse_name %>
<%= f.label :english_level, 'English Level:' %>
<%= student_detail_field.select :english_level, ['Low', 'Medium Low', 'Medium', 'Medium High', 'High'] %>
<%= f.label :expectations, 'What are your expectations for the program?:' %>
<%= student_detail_field.text_area :expectations %>
<%= f.label :length_of_stay, 'About how long will you be in town?:' %>
<%= student_detail_field.select :length_of_stay, ['Less than 1 Year', '1 Year', '1-2 Years', '2-3 Years', '3+ Years'] %>
<%= f.label :exact_length, 'Please tell us exactly how long you plan to be in town:' %>
<%= student_detail_field.text_area :exact_length %>
<% end %>
这里是我的控制器:
class ParticipantsController < ApplicationController
def new
@participant= Participant.new
@studentdetail= @participant.build_student_detail(participant: @participant)
end
def new2
@participant= Participant.new
@volunteerdetails= @participant.build_student_detail(participant: @participant)
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, student_detail_attributes: [:nationality, :religion, :need_ride,
:has_spouse, :spouse_name, :english_level, :expectations, :length_of_stay, :exact_length, :volunteer_id,
:matched, :returned_home, :participant_id])
end
end
模型:
class Participant < ApplicationRecord
validates :last_name, presence: true
# validates :gender, inclusion: { in: %w(male female) }
validates :nationality, presence: true
validates :phone, presence: true
has_one :volunteer_detail, :dependent => :destroy
accepts_nested_attributes_for :volunteer_detail, :allow_destroy => :true
has_one :student_detail, :dependent => :destroy
accepts_nested_attributes_for :student_detail, :allow_destroy => :true
end
这里是服务器日志:
Completed 500 Internal Server Error in 148ms (ActiveRecord: 0.0ms)
ActionView::Template::Error (undefined method `exact_length' for #<StudentDetail:0x000000000d1097f0>):
58: <%= f.label :length_of_stay, 'About how long will you be in Nashville?:' %>
59: <%= student_detail_field.select :length_of_stay, ['Less than 1 Year', '1 Year', '1-2 Years', '2-3 Years', '3+ Years'] %>
60: <%= f.label :exact_length, 'Please tell us exactly how long you plan to be in Nashville:' %>
61: <%= student_detail_field.text_area :exact_length %>
62: <% end %>
63:
64: <br>
app/views/participants/new.html.erb:61:in `block (2 levels) in _app_views_participants_new_html_erb__1041809053_109576820'
app/views/participants/new.html.erb:46:in `block in _app_views_participants_new_html_erb__1041809053_109576820'
app/views/participants/new.html.erb:11:in `_app_views_participants_new_html_erb__1041809053_109576820'
Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7.1/lib/action_dispatch/middleware/templates/rescues/template_error.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 (6.0ms)
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.8ms)
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/template_error.html.erb within rescues/layout (1023.7ms)
我希望将信息提交给使用一种形式这两种模式,但对于依赖模型的属性显示为一个未定义的方法。
试着改变你的qazxsw POI块qazxsw POI内qazxsw POI