gpt4 book ai didi

ruby-on-rails - 具有嵌套属性的复杂 Rails 表单 (Rails 5)

转载 作者:行者123 更新时间:2023-12-02 04:37:46 24 4
gpt4 key购买 nike

我有一个表单,它保存主要属性,但不保存其嵌套属性。我已经研究了很多文档,并且似乎在正确地做事,但仍然收到我的嵌套属性“必须存在”的错误。

我的面试属性正确保存到数据库中,但我的日志显示“Unpermitted parameters: student, parents”

为简洁起见,我的代码进行了修改,但我仍会尽量做到足够彻底,以便就可能出现的问题找到一些方向......

模型(我包括 accepts_nested_attributes for :student, :parents)

:student是单数,因为它有has_one关系

:parents是复数,因为它有has_many关系

class Interview < ApplicationRecord
has_one :student
has_many :parents

accepts_nested_attributes_for :student, :parents
end

class Student < ApplicationRecord
belongs_to :interview
end

class Parent < ApplicationRecord
belongs_to :interview
end

Controller

class InterviewsController < ApplicationController

def index
@interviews = Interview.all
end

def show
@interview = Interview.find(params[:id])
end

def new
@interview = Interview.new
@interview.build_student
2.times { @interview.parents.build }
end

def create
@interview = Interview.new(interview_params)

if @interview.save
redirect_to @interview
else
render :action => 'new'
end
end



private

def interview_params
params.require(:interview).permit(:date_today, :date_contact, :purpose_of_call, :problems_start_date, :cause, :violence, :running_away, :police_contact, :suicide, :self_harm, :other_info, :testing, :hospitalization, :medications, :school_problems, :teacher_relationships, :parent_goals, :notes,
student_attributes: [:id, :name, :age, :height, :weight, :dob, :interview_id],
parents_attributes: [:id, :name, :relationship, :parentage, :address, :phone_home, :phone_work, :phone_mobile, :phone_mobile, :email, :employer, :notes, :interview_id] )
end

end

形式(重要位)

<%= form_for(@interview) do |f| %>

<p class="inline">
<%= f.label :date_today, 'Today\'s Date' %>
<%= f.date_select(:date_today, :order => [:month, :day, :year], :start_year => 2000, :end_year => Date.today.year) %>
</p>

<p class="inline float_right">
<%= f.label :date_contact, 'Initial Contact' %>
<%= f.date_select(:date_contact, :order => [:month, :day, :year], :start_year => 2000, :end_year => Date.today.year) %>
</p>

<%= f.fields_for :student do |student_form| %>
<p><%= student_form.text_field :name, placeholder: 'Name' %></p>

<p class="inline">
<%= student_form.label :age %>
<%= student_form.text_field :age %>
</p>

<p class="inline">
<%= student_form.label :height %>
<%= student_form.text_field :height %>
</p>

<p class="inline">
<%= student_form.label :weight %>
<%= student_form.text_field :weight %>
</p>

///// removed for brevity /////

<% end %>

<%= f.fields_for :parents do |parent_form| %>
<%= render 'parents', :f => parent_form %>
<% end %>

父部分

<p>
<%= f.label :name, 'Name' %>
<%= f.text_field :name %>
</p>

//// and more of the same /////

路线

resources :interviews do
resources :student
resources :parents
end

可以在此处找到网站表单(当前状态):www.compassconsultingwi.com/interviews/new

github 的链接可以在这里找到:https://github.com/plantoteachme/compassconsultingwi

Params 返回这个 ..

Parameters: {"utf8"=>"✓", "authenticity_token"=>"nU4WM2RO5GJd36eaSLHMxhRQCOnY8EPjDhUdFBHlYGkcw6H7/Oc5y7kFx0HMU9nm5cc47ZZZBDW6oQ2QNF5yhA==", "interview"=>{"date_today(2i)"=>"11", "date_today(3i)"=>"16", "date_today(1i)"=>"2016", "date_contact(2i)"=>"10", "date_contact(3i)"=>"23", "date_contact(1i)"=>"2016", "student"=>{"name"=>"John", "age"=>"12", "height"=>"5 feet", "weight"=>"123 lbs", "dob(2i)"=>"3", "dob(3i)"=>"13", "dob(1i)"=>"2004", "strengths"=>"Great with his siblings", "weaknesses"=>"Lazy", "likes"=>"Food", "dislikes"=>"Chores", "medical_prolems"=>"ADD", "religous_training"=>"Catholic", "ethnic_issues"=>"none", "grade_level"=>"6"}, "parents"=>{"name"=>"Jamie", "relationship"=>"Mom", "parentage"=>"Strict", "address"=>"Miwaukee Wi", "phone_home"=>"555-1000", "phone_work"=>"555-1001", "phone_mobile"=>"555-1002", "email"=>"jj@jj.com", "employer"=>"Googleer", "notes"=>"PhD in Computer Science"}, "purpose_of_call"=>"Depression causing suicidal tendencies", "problems_start_date"=>"When we moved from Nigeria last year", "cause"=>"Relocating", "violence"=>"none", "running_away"=>"no", "police_contact"=>"no", "suicide"=>"Hasn't acted on it, but talks about it", "self_harm"=>"Minor bruising from \"sports\"", "other_info"=>"", "testing"=>"Yes, for ADD", "hospitalization"=>"no", "medications"=>"Regeline", "school_problems"=>"Getting bullied", "teacher_relationships"=>"Strained", "parent_goals"=>"Improve self awareness", "notes"=>"Our family was in Nigeria for mission work"}, "button"=>""}

不允许的参数:学生、家长

最佳答案

尝试使用cocoon gem。

您可以使用 cocoon gem 的 link_to_add_association 方法构建模型对象。

此外,您可以使用 link_to_remove_association 移除对象

这里有一个完整的示例:https://github.com/nathanvda/cocoon/wiki/ERB-examples

关于ruby-on-rails - 具有嵌套属性的复杂 Rails 表单 (Rails 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40621363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com