Ruby on rails 为什么我的表单只提交一个模型&x27;谁的数据?
我有一个表格打算提交两个模型,第一个模型Ruby on rails 为什么我的表单只提交一个模型&x27;谁的数据?,ruby-on-rails,form-for,fields-for,Ruby On Rails,Form For,Fields For,我有一个表格打算提交两个模型,第一个模型有另一个模型的一个,Student\u Detail 这里的许多用户帮助我解决了一些我已经遇到的错误,现在我的表单提交成功,但只提交了主模型Participant 我已经提交了几次表单,它存储了参与者模型的所有直接属性信息。在活动管理中,参与者模型数据填充,但不存在学生详细信息记录 在控制台中,Student\u Detail数据不存在 表格: 型号: class Participant < ApplicationRe
有另一个模型的一个,Student\u Detail
这里的许多用户帮助我解决了一些我已经遇到的错误,现在我的表单提交成功,但只提交了主模型Participant
我已经提交了几次表单,它存储了参与者
模型的所有直接属性信息。在活动管理中,参与者
模型数据填充,但不存在学生详细信息
记录
在控制台中,Student\u Detail
数据不存在
表格:
型号:
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
课堂参与者:destroy
接受:志愿者\u详细信息的\u嵌套\u属性,:允许\u销毁=>:true
有一个:学生详细信息,:dependent=>:destroy
接受\u嵌套的\u属性\u for:student\u detail,:allow\u destroy=>:true
结束
-
class StudentDetail
控制器:
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
class ParticipantsController
如果您有任何建议或建议,我将不胜感激。我正在尝试在提交表单时使用数据填充这两个模型,并且当前主模型是唯一有效的模型 我认为这里的问题在于视图中行字段的语法
更改@participant.student\u detail do | student\u detail\u field |
至f.fields\u:student\u detail,@participant.student\u detail do | student\u detail\u field |
我敢打赌,如果你在你的控制器中放入binging.pry或puts语句来查看你的参数,那么student\u detail\u属性甚至无法访问你的参数。您只需在控制器中插入放置参数。检查以检查参数是否如您所期望的那样通过。您的强params语法看起来和模型一样正确,因此我认为这是您的视图的一个问题
编辑
这里有一些其他的东西可以尝试。正如我在评论中提到的,我通常不会这样做
@participant=participant.create(参与者参数)
我只想:
@participant = Participant.new(participant_params)
if @participant.save
#etc
end
上面的内容也会稍微清理一下您的代码。(我还注意到,在这个操作中,您没有说明当参与者不保存时应该做什么——作为一个旁注,您应该编写一些代码来处理这个问题)
此外,如果上述两种方法都不起作用,有时我会发现使用accept_嵌套的_属性,我必须将模型设置为彼此的“反向”。有关更多信息,请参阅本文:--关于has_许多,但我认为它也应该与has_一有关--尝试参与者模型:
有一个:学生详细信息,:依赖=>:破坏,与::参与者相反
看看上面的任何一个(或两者的组合)是否得到了嵌套模型的保存。我认为这里的问题在于视图中行字段的语法
更改@participant.student\u detail do | student\u detail\u field |
至f.fields\u:student\u detail,@participant.student\u detail do | student\u detail\u field |
我敢打赌,如果你在你的控制器中放入binging.pry或puts语句来查看你的参数,那么student\u detail\u属性甚至无法访问你的参数。你可以直接插入<代码>
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
@participant = Participant.new(participant_params)
if @participant.save
#etc
end