Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更新复制记录并添加新记录_Javascript_Jquery_Ruby On Rails_Sql Update - Fatal编程技术网

Javascript 更新复制记录并添加新记录

Javascript 更新复制记录并添加新记录,javascript,jquery,ruby-on-rails,sql-update,Javascript,Jquery,Ruby On Rails,Sql Update,我有一个叫做“投资组合”的模型和一个叫做“投资”的嵌套模型 以下是公文包编辑摘要视图: <table style="margin-bottom: 6px"> <thead> <tr>

我有一个叫做“投资组合”的模型和一个叫做“投资”的嵌套模型

以下是公文包编辑摘要视图:

<table style="margin-bottom: 6px">                           
   <thead>                                                    
   <tr>                                                       
     <td>Ticker Symbol</td>                                   
     <td>Shares</td>                                          
     <td>Remove Investment</td>                               
   </tr>                                                      
   </thead>                                                   
   <%= f.fields_for :investments do |builder| %>              
       <%= render 'investments/table_form', f: builder  %>    
   <% end %>                                                  
</table>   
has_many :investments
accepts_nested_attributes_for :investments, allow_destroy: true, reject_if: proc { |attributes| attributes['ticker'].blank? }
咖啡

jQuery ->
  $('form').on 'click', '.remove_fields', (event) ->
    $(this).prev('input[type=hidden]').val('1')
    $(this).closest('tr').hide()
    event.preventDefault()
当我单击“删除”链接时,它会隐藏投资,并将隐藏字段的值更改为1,这是正确的。但这并不能挽救它

投资组合模型摘要:

<table style="margin-bottom: 6px">                           
   <thead>                                                    
   <tr>                                                       
     <td>Ticker Symbol</td>                                   
     <td>Shares</td>                                          
     <td>Remove Investment</td>                               
   </tr>                                                      
   </thead>                                                   
   <%= f.fields_for :investments do |builder| %>              
       <%= render 'investments/table_form', f: builder  %>    
   <% end %>                                                  
</table>   
has_many :investments
accepts_nested_attributes_for :investments, allow_destroy: true, reject_if: proc { |attributes| attributes['ticker'].blank? }
提交编辑表单后,参数:

Parameters:

{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"GX4xeCdH9iNIViYb7oR+c9/1MqHVOM+2iceFlztqrfukblGkJBZgPRoYC5XNIyMK9b/o/tZIgnUBe/0qafXyvw==",
"portfolio"=>{"nickname"=>"Wassef",
"management_fee"=>"1.0",
"id"=>"2",
"investments_attributes"=>{"0"=>{"ticker"=>"WABIX",
"quantity"=>"1000",
"_destroy"=>"false",
"id"=>"6"},
"1"=>{"ticker"=>"WABIX",
"quantity"=>"1000",
"_destroy"=>"1",
"id"=>"7"},
"2"=>{"ticker"=>"NBQAX",
"quantity"=>"1000",
"_destroy"=>"1",
"id"=>"8"},
"3"=>{"ticker"=>"WABIX",
"quantity"=>"1000",
"_destroy"=>"false",
"id"=>"49"}}},
"commit"=>"Update Portfolio",
"id"=>"2"}

应该删除ID 7和8,但它们不是。

解决了这个问题。那是在公文包里的。我需要:身份证在里面。将其更改为:

def portfolio_params
  params.require(:portfolio).permit(:nickname, :management_fee,    investments_attributes: [:id, :ticker, :quantity, '_destroy'])
end

它可以工作。

关于更新-你能在其他字段之前添加带有投资组合id的隐藏字段,并告诉我们会发生什么吗?我想这可能会引起一些问题。我应该把这个放在哪里?是局部的还是局部的?