为什么我的表单只提交一个模型的数据?
Why would my form submit only one model's data?
我有一个表格要提交两个模型,第一个模型 has_one
另一个模型 Student_Detail
。
这里的许多用户帮助我解决了一些我已经遇到的错误,现在我的表单提交成功,但只提交了主要模型,Participant
。
我已经提交了几次表单,它存储了所有属于 Participant
模型的直接属性的信息。在活动管理员中,Participant
模型数据已填充,但不存在 Student_Detail
记录。
在控制台中,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.label :role, 'Role:' %>
<%= f.select :role, ['Reader'] %> -->
<br>
<br>
<%= f.fields_for @participant.student_detail do |student_detail_field| %>
<%= student_detail_field.label :nationality, 'Nationality:' %>
<%= student_detail_field.text_field :nationality %>
<%= student_detail_field.label :religion, 'Religion:' %>
<%= student_detail_field.text_field :religion %>
<%= student_detail_field.label :need_ride, 'Do you need help getting to the church building?' %>
<%= student_detail_field.select :need_ride, ['Yes','No'] %>
<br>
<br>
<%= student_detail_field.label :has_spouse, 'Marital Status:' %>
<%= student_detail_field.select :has_spouse, ['married', 'single'] %>
<br>
<br>
<%= student_detail_field.label :spouse_name, "If married, spouse's name:" %>
<%= student_detail_field.text_field :spouse_name %>
<%= student_detail_field.label :english_level, 'English Level:' %>
<%= student_detail_field.select :english_level, ['Low', 'Medium Low', 'Medium', 'Medium High', 'High'] %>
<%= student_detail_field.label :expectation, 'What are your expectations for the program?:' %>
<%= student_detail_field.text_area :expectation %>
<%= student_detail_field.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'] %>
<%= student_detail_field.label :exact_length, 'Please tell us exactly how long you plan to be in town:' %>
<%= student_detail_field.text_area :exact_length %>
<% end %>
型号:
class Participant < ApplicationRecord
validates :last_name, presence: true
# validates :gender, inclusion: { in: %w(male female) }
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
-
class StudentDetail < ApplicationRecord
belongs_to :participant
end
控制器:
class ParticipantsController < ApplicationController
def new
@participant= Participant.new
@studentdetail= @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, :expectation, :length_of_stay, :exact_length, :volunteer_id,
:matched, :returned_home, :participant_id])
end
end
欢迎任何建议或建议,我正在尝试让两个模型在提交表单时填充数据,目前只有主模型有效。
我认为这里的问题在于您认为 fields_for 行的语法。
改变f.fields_for @participant.student_detail do |student_detail_field|
到f.fields_for :student_detail, @participant.student_detail do |student_detail_field|
我敢打赌,如果您在控制器中放入 binging.pry 或 puts 语句来查看您的参数,student_detail_attributes 甚至不会获取您的参数。您只需在控制器中插入 puts params.inspect
即可检查参数是否按您预期的那样通过。您强大的参数语法看起来和模型一样正确,所以我认为这是您的观点的问题。
编辑
这里有一些其他的尝试。正如我在评论中提到的,我通常不会这样做
@participant = Participant.create(participant_params)
我会这样做:
@participant = Participant.new(participant_params)
if @participant.save
#etc
end
上面的代码也会稍微清理一下你的代码。 (我还注意到,您在此操作中没有说明当参与者未保存时该怎么做 - 作为旁注,您应该编写一些代码来处理该问题)
此外,如果以上方法均无效,有时我发现 accept_nested_attributes 我必须将模型设置为彼此的 "inverse"。有关更多信息,请参阅这篇文章:https://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through -- 它是关于 has_many 但我认为它也应该与 has_one 有关 -- 在参与者模型中尝试:
has_one :student_detail, :dependent => :destroy, inverse_of: :participant
查看以上任一(或两者的组合)是否获得嵌套模型保存。
我有一个表格要提交两个模型,第一个模型 has_one
另一个模型 Student_Detail
。
这里的许多用户帮助我解决了一些我已经遇到的错误,现在我的表单提交成功,但只提交了主要模型,Participant
。
我已经提交了几次表单,它存储了所有属于 Participant
模型的直接属性的信息。在活动管理员中,Participant
模型数据已填充,但不存在 Student_Detail
记录。
在控制台中,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.label :role, 'Role:' %>
<%= f.select :role, ['Reader'] %> -->
<br>
<br>
<%= f.fields_for @participant.student_detail do |student_detail_field| %>
<%= student_detail_field.label :nationality, 'Nationality:' %>
<%= student_detail_field.text_field :nationality %>
<%= student_detail_field.label :religion, 'Religion:' %>
<%= student_detail_field.text_field :religion %>
<%= student_detail_field.label :need_ride, 'Do you need help getting to the church building?' %>
<%= student_detail_field.select :need_ride, ['Yes','No'] %>
<br>
<br>
<%= student_detail_field.label :has_spouse, 'Marital Status:' %>
<%= student_detail_field.select :has_spouse, ['married', 'single'] %>
<br>
<br>
<%= student_detail_field.label :spouse_name, "If married, spouse's name:" %>
<%= student_detail_field.text_field :spouse_name %>
<%= student_detail_field.label :english_level, 'English Level:' %>
<%= student_detail_field.select :english_level, ['Low', 'Medium Low', 'Medium', 'Medium High', 'High'] %>
<%= student_detail_field.label :expectation, 'What are your expectations for the program?:' %>
<%= student_detail_field.text_area :expectation %>
<%= student_detail_field.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'] %>
<%= student_detail_field.label :exact_length, 'Please tell us exactly how long you plan to be in town:' %>
<%= student_detail_field.text_area :exact_length %>
<% end %>
型号:
class Participant < ApplicationRecord
validates :last_name, presence: true
# validates :gender, inclusion: { in: %w(male female) }
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
-
class StudentDetail < ApplicationRecord
belongs_to :participant
end
控制器:
class ParticipantsController < ApplicationController
def new
@participant= Participant.new
@studentdetail= @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, :expectation, :length_of_stay, :exact_length, :volunteer_id,
:matched, :returned_home, :participant_id])
end
end
欢迎任何建议或建议,我正在尝试让两个模型在提交表单时填充数据,目前只有主模型有效。
我认为这里的问题在于您认为 fields_for 行的语法。
改变f.fields_for @participant.student_detail do |student_detail_field|
到f.fields_for :student_detail, @participant.student_detail do |student_detail_field|
我敢打赌,如果您在控制器中放入 binging.pry 或 puts 语句来查看您的参数,student_detail_attributes 甚至不会获取您的参数。您只需在控制器中插入 puts params.inspect
即可检查参数是否按您预期的那样通过。您强大的参数语法看起来和模型一样正确,所以我认为这是您的观点的问题。
编辑
这里有一些其他的尝试。正如我在评论中提到的,我通常不会这样做
@participant = Participant.create(participant_params)
我会这样做:
@participant = Participant.new(participant_params)
if @participant.save
#etc
end
上面的代码也会稍微清理一下你的代码。 (我还注意到,您在此操作中没有说明当参与者未保存时该怎么做 - 作为旁注,您应该编写一些代码来处理该问题)
此外,如果以上方法均无效,有时我发现 accept_nested_attributes 我必须将模型设置为彼此的 "inverse"。有关更多信息,请参阅这篇文章:https://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through -- 它是关于 has_many 但我认为它也应该与 has_one 有关 -- 在参与者模型中尝试:
has_one :student_detail, :dependent => :destroy, inverse_of: :participant
查看以上任一(或两者的组合)是否获得嵌套模型保存。