gpt4 book ai didi

ruby - 使用嵌套属性创建相关数据

转载 作者:太空宇宙 更新时间:2023-11-03 16:47:43 25 4
gpt4 key购买 nike

应用程序接口(interface)。现在需要插入一条有关联记录的记录。我阅读了嵌套属性但没有任何想法。不太明白。

我在 rails mvc 项目中尝试了一个示例,但即使我无法在一对一映射本身中创建关联记录。请指导我执行此操作的步骤或任何带有示例的好文章?

下面是我的作品

我的模型是

class Instructor < ActiveRecord::Base
has_one :office_assignment
has_many :departments
has_and_belongs_to_many :courses

accepts_nested_attributes_for :office_assignment
end

class OfficeAssignment < ActiveRecord::Base
belongs_to :instructor
end

我的 Controller 创建方法

def new
@instructor = Instructor.new
end

def create
@instructor = Instructor.new(instructor_params)

respond_to do |format|
if @instructor.save

format.html { redirect_to @instructor, notice: 'Instructor was successfully created.' }
format.json { render :show, status: :created, location: @instructor }
else
format.html { render :new }
format.json { render json: @instructor.errors, status: :unprocessable_entity }
end
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_instructor
@instructor = Instructor.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def instructor_params
params.require(:instructor).permit(:LastName, :FirstMidName, :HireDate, office_assignment_attributes: [:Location])
end

我的创建表单

<%= form_for @instructor, :html => { :class => "form-horizontal instructor" } do |f| %>

<% if @instructor.errors.any? %>
<div id="error_expl" class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><%= pluralize(@instructor.errors.count, "error") %> prohibited this instructor from being saved:</h3>
</div>
<div class="panel-body">
<ul>
<% @instructor.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>

<div class="form-group">
<%= f.label :LastName, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :LastName, :class => 'form-control' %>
</div>
<%= error_span(@instructor[:LastName]) %>
</div>
<div class="form-group">
<%= f.label :FirstMidName, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :FirstMidName, :class => 'form-control' %>
</div>
<%= error_span(@instructor[:FirstMidName]) %>
</div>
<div class="form-group">
<%= f.label :HireDate, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :HireDate, :class => 'form-control' %>
</div>
<%= error_span(@instructor[:HireDate]) %>
</div>

<div class="form-group">
<%= f.label :Location, :class => 'control-label' %>
<div class="controls">
<input type="text" name="office_assignment_Location" class ="form-control">
</div>
<%= error_span(@instructor[:HireDate]) %>
</div>


<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
instructors_path, :class => 'btn btn-default' %>

<% end %>

请指导我..

编辑:

html 文件的变化

<%= f.fields_for :office_assignment_Location do |loc| %>
<div class="form-group">
<%= loc.label :Location, :class => 'control-label' %>
<div class="controls">
<%= loc.text_field :Location %>
</div>
</div>
<%= loc.link_to_add "Add Location", :office_assignment_Location , :class=>'btn btn-primary'%>
<% end %>

我在 Controller 中的参数

def instructor_params
params.require(:instructor).permit(:LastName, :FirstMidName, :HireDate, office_assignment_attributes: [:id, :Location ,:_destroy])
end

我的架构

create_table "instructors", force: :cascade do |t|
t.string "LastName", limit: 255
t.string "FirstMidName", limit: 255
t.date "HireDate"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "office_assignments", primary_key: "Instructor_Id", force: :cascade do |t|
t.string "Location", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_foreign_key "office_assignments", "instructors", column: "Instructor_Id"

最佳答案

仅供引用:属性名称应以小写字母而非大写字母开头。

这里有一些变化:

class Instructor < ActiveRecord::Base
has_one :office_assignment , :dependent => :destroy # add dependent destroy
has_many :departments
has_and_belongs_to_many :courses

accepts_nested_attributes_for :office_assignment, :reject_if => :all_blank, :allow_destroy => true # add allow destroy
end

强参数的一些变化:

 def instructor_params
params.require(:instructor).permit(:LastName, :FirstMidName, :HireDate, office_assignment_attributes: [:id, :Lesson ,:_destroy])
end

注意:不要忘记在 office_assignment_attributes 中添加 :id:_destroy

现在在 View 中(form_template):

取而代之的是:

<div class="form-group">
<%= f.label :Location, :class => 'control-label' %>
<div class="controls">
<input type="text" name="office_assignment_Location" class ="form-control">
</div>
<%= error_span(@instructor[:HireDate]) %>
</div>

应该是:

<%= nested_form_for @instructor, :html => { :class => "form-horizontal instructor" } do |f| %>

......
.....

<%= f.fields_for :office_assignment do |loc| %>
<div class="form-group">
<%= loc.label :Location, :class => 'control-label' %>
<div class="controls">
<%= loc.text_field :Location %>
</div>
</div>
<% end %>
<%= f.link_to_add "Add Location", :office_assignment , :class=>'btn btn-primary'%>

您必须像这样创建嵌套表单。希望对您有所帮助。

注意:在表单中使用 nested_form_for 而不是 form_for:供您引用:

关于ruby - 使用嵌套属性创建相关数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789151/

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