数据未以嵌套形式保存

问题描述 投票:0回答:1

我正在尝试实现嵌套表单。

供参考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作为我的主窗体的主键

先感谢您

ruby-on-rails-4 nested-forms
1个回答
0
投票

似乎给出的关联是错误的现在工作正常

我改变了我的迁移如下

def change
   create_table :shyain_rirekis do |t|
       t.integer    :wfs_id 
       t.string     :emp_type
       t.string     :company_name
   end
end

模型变化

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

查看更改

<%= 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>

部分变化

<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>
© www.soinside.com 2019 - 2024. All rights reserved.