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

时间:2015-09-01 23:35:21

标签: javascript jquery ruby-on-rails sql-update

我有一个名为'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>   

部分(投资/ 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()

当我点击“删除”链接时,它隐藏了投资,并将隐藏字段的值更改为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,但不是。

1 个答案:

答案 0 :(得分:0)

想出来。它在var memberDescription = result.Members["description"] as PSObject[]; if (memberDescription != null && memberDescription.Value != null){ foreach (PSObject d in memberDescription.Value ) { paramDesc.Append(d.Members["Text"].Value); } } 。我需要portfolio_params。将其更改为:

:id

它有效。