我正在尝试实现嵌套表单。
供参考https://www.driftingruby.com/episodes/nested-forms-from-scratch
但是在此视频中,他们使用了simple_form_for 但是我用fields_for
现在我的问题是保存了主要形式的数据,但没有保存嵌套形式的数据。
我已正确检查,数据已从视图传递到嵌套表单的控制器
我的控制器
class TShyainTouyouJoshinshosController < ApplicationController
def new
@t_shyain_touyou_joshinsho = TShyainTouyouJoshinsho.new
end
def create
@t_shyain_touyou_joshinsho = TShyainTouyouJoshinsho.new(t_shyain_touyou_joshinsho_params)
@t_shyain_touyou_joshinsho.assign_attributes(:created_by => current_m_user_login.user_id)
respond_to do |format|
if @t_shyain_touyou_joshinsho.save
format.html { redirect_to dashboard_path, notice: '登録は完了しました。' }
format.json { render :show, status: :created, location: @t_shyain_touyou_joshinsho }
else
format.html { render :new }
format.json { render json: @t_shyain_touyou_joshinsho.errors, status: :unprocessable_entity }
end
end
end
private
def t_shyain_touyou_joshinsho_params
params.require(:t_shyain_touyou_joshinsho).permit(:wfs_id,:joshinsho_bi,shyain_rirekis_attributes: [:id,:emp_type,:company_name,:_destroy])
end
end
我的模型
class TShyainTouyouJoshinsho < ActiveRecord::Base
self.primary_key='wfs_id'
has_many :shyain_rirekis, foreign_key: 't_shyain_touyou_joshinsho_id', dependent: :destroy
accepts_nested_attributes_for :shyain_rirekis, allow_destroy: true , reject_if: :all_blank
end
class ShyainRireki < ActiveRecord::Base
belongs_to :t_shyain_touyou_joshinsho, :foreign_key => 't_shyain_touyou_joshinsho_id'
end
查看主表格
<%= link_to_add_nested_form_shyain('✙ 社員履歴', f, :shyain_rirekis, class: 'btn btn-success') %>
<div class="append_nested_form_fields">
</div>
查看部分
<%= f.fields_for :shyain_rirekis do |ff| %>
<tr>
<td><%= ff.select :emp_type,TShyainTouyouJoshinsho::EMP_TYPE,{}, class: 'form-control' , :disabled => @disabled_field %></td>
<td><%= ff.text_field :company_name, class: 'form-control' , :disabled => @disabled_field %></td>
<td><%= ff.hidden_field :_destroy %>
<%= link_to '削除' ,'#' , class: " btn btn-xs btn-danger remove_record" %>
</td>
</tr>
<% end %>
我的助手
def link_to_add_nested_form_shyain(name, f, association, **args)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize, f: builder, commute_object: new_object)
end
link_to(name, '#', class: "add_nested_shyain_rireki " + args[:class], data: {id: id, fields: fields.gsub("\n", "")})
end
我的JAVASCRIPT
$(document).ready(function(){
$('form').on('click', '.remove_record', function(event) {
$(this).prev('input[type=hidden]').val('1');
$(this).closest('tr').hide();
return event.preventDefault();
});
$('form').on('click', '.add_nested_shyain_rireki', function(event) {
var regexp, time;
time = new Date().getTime();
regexp = new RegExp($(this).data('id'), 'g');
$('.append_nested_form_fields').append($(this).data('fields').replace(regexp, time));
return event.preventDefault();
});
});
迁移文件
def change
create_table :t_shyain_touyou_joshinshos do |t|
t.integer :wfs_id
t.date :joshinsho_bi
end
end
def change
create_table :shyain_rirekis do |t|
t.references :t_shyain_touyou_joshinsho, index: true, foreign_key: true
t.string :emp_type
t.string :company_name
end
end
我将wfs_id用作主表单的主键
先谢谢您
答案 0 :(得分:0)
It seems that the association given was wrong It works fine now
I changed my migrations as follows
def change
create_table :shyain_rirekis do |t|
t.integer :wfs_id
t.string :emp_type
t.string :company_name
end
end
MODEL CHANGES
class TShyainTouyouJoshinsho < ActiveRecord::Base
self.primary_key='wfs_id'
has_many :shyain_rirekis, foreign_key: 'wfs_id', dependent: :destroy
accepts_nested_attributes_for :shyain_rirekis, allow_destroy: true , reject_if: :all_blank
end
class ShyainRireki < ActiveRecord::Base
belongs_to :t_shyain_touyou_joshinsho, :foreign_key => 'wfs_id'
end
VIEW CHANGES
<%= link_to_add_nested_form_shyain('✙ 社員履歴', f, :shyain_rirekis, class: 'btn btn-success') %>
<div class="append_nested_form_fields">
<%= f.fields_for :shyain_rirekis do |builder| %>
<%= render 'shyain_rireki', f: builder %>
<% end %>
</div>
PARTIAL CHANGES
<tr>
<td><%= f.select :emp_type,TShyainTouyouJoshinsho::EMP_TYPE,{}, class: 'form-control' , :disabled => @disabled_field %></td>
<td><%= f.text_field :company_name, class: 'form-control' , :disabled => @disabled_field %></td>
<td><%= f.hidden_field :_destroy %>
<%= link_to '削除' ,'#' , class: " btn btn-xs btn-danger remove_record" %>
</td>
</tr>