更新复制记录并添加新的

Update replicates record and adds new

我有一个名为 'Portfolio' 的模型和一个名为 'Investment'

的嵌套模型

这是投资组合编辑的视图(删节):

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

和部分 ( investments/table_form )

<tr>
  <td><%= f.text_field :ticker, class: 'form-control' %></td>
  <td><%= f.text_field :quantity, class: 'form-control' %></td>
  <td><span style="padding-top:-20px;"><%= f.hidden_field :_destroy %><%= link_to 'remove', '#', class: 'remove_fields btn btn-danger' %></span></td>
</tr>

可以创建新的投资组合,但是当我编辑之前的投资组合时,它需要之前的投资并添加所有新的投资。 EG:原始投资:A、B、C。编辑投资组合并添加新投资 D。将 A、B、C、A、B、C、D 保存为投资组合的投资。

更新操作:

before_action :set_portfolio, only: [:show, :edit, :destroy, :update]

def update
  nickname = @portfolio.nickname
  if @portfolio.update_attributes(portfolio_params)
    redirect_to portfolios_path, notice: "The portfolio \"#{nickname}\" was successfully updated."
  else
    render :edit
  end
end

private
def set_portfolio
  @portfolio = Portfolio.find(params[:id])
end

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

portfolio.coffee

jQuery ->
  $('form').on 'click', '.remove_fields', (event) ->
    $(this).prev('input[type=hidden]').val('1')
    $(this).closest('tr').hide()
    event.preventDefault()

当我单击 link "Remove" 时,它隐藏了投资并将隐藏字段的值更改为 1,这是正确的。但是没有保存。

投资组合模型(删节):

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 应该被删除,但他们没有。

想通了。它在 portfolio_params 中。我需要 :id 在里面。更改为:

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

而且有效。