我有一个模型declaration
,它有嵌套属性cost
。使用成本创建声明工作正常,但在更新声明时,更新的成本值不会更新。有什么问题?
if
子句的update_attributes
部分。UPDATE
个查询。:id
添加到:declaration_params
。declaration.rb:
class Declaration < ActiveRecord::Base
has_many :costs
accepts_nested_attributes_for :costs, reject_if: :all_blank, allow_destroy: true
end
cost.rb:
class Cost < ActiveRecord::Base
belongs_to :declaration
end
declarations_controller.rb:
class DeclarationsController < ApplicationController
def edit
@declaration = Declaration.find(params[:id])
end
def update
@declaration = Declaration.find(params[:id])
if @declaration.update_attributes(declaration_params)
redirect_to user_declarations_path, notice: I18n.t('.declaration.message_update')
else
render action: "edit"
end
end
def declaration_params
params.require(:declaration).permit(:approval_date, :submit_date, :status, :user_id, :company_id, :declaration_number, costs_attributes: [:id, :description, :country, :amount_whole, :amount_dec, :amount_foreign_whole, :amount_foreign_dec, :cost_date, :attachment, :projectuser_id, :_destroy])
end
end
参数:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "declaration"=>{"user_id"=>"102", "costs_attributes"=>{"0"=>{"cost_date(3i)"=>"14", "cost_date(2i)"=>"10", "cost_date(1i)"=>"2015", "projectuser_id"=>"", "description"=>"test2", "amount_whole"=>"22", "amount_dec"=>"22", "amount_foreign_whole"=>"88", "amount_foreign_dec"=>"88", "country"=>"USA", "id"=>"2224"}, "1"=>{"cost_date(3i)"=>"1", "cost_date(2i)"=>"1", "cost_date(1i)"=>"2015", "projectuser_id"=>"", "description"=>"test", "amount_whole"=>"99", "amount_dec"=>"99", "amount_foreign_whole"=>"55", "amount_foreign_dec"=>"55", "country"=>"engeland", "id"=>"2223"}}}, "commit"=>"Opslaan", "user_id"=>"102", "id"=>"1147"}
_form.html.haml:
%table.table#declarations
%tbody
= f.fields_for(:costs) do |cost|
= render 'cost_fields', f: cost
%table.table.insert_row
_cost_fields.html.haml:
%tr.cost_line
%th{ colspan: 2}
%tr
%td= I18n.t('.cost.cost_date')
%td= f.date_select :cost_date, { include_blank: true, default: nil, start_year: 1.year.ago.year, end_year: DateTime.now.year }
%tr
%td= I18n.t('.cost.project')
%td= f.collection_select :projectuser_id, @projectusers, :id, :full_name, include_blank: true
%tr
%td= I18n.t('.cost.description')
%td= f.text_field :description, class: "input-xlarge"
%tr
%td= I18n.t('.cost.amount')
%td
.amount= f.text_field :amount_whole, value: f.object.amount.to_s.split('.').first, class: "input-mini"
.amount_divider= ","
.amount_foreign= f.text_field :amount_dec, value: f.object.amount.to_s.split('.').last, class: "input-mini"
%tr
%td= I18n.t('.cost.amount_foreign')
%td
.amount= f.text_field :amount_foreign_whole, value: f.object.amount_foreign.to_s.split('.').first, class: "input-mini"
.amount_divider= ","
.amount_foreign= f.text_field :amount_foreign_dec, value: f.object.amount_foreign.to_s.split('.').last, class: "input-mini"
%tr
%td= I18n.t('.cost.country_long')
%td
= f.text_field :country, class: "input-medium", placeholder: "b.v. Engeland"
%tr
%td= I18n.t('.cost.attachment')
- if f.object.new_record?
%td= f.file_field :attachment
- else
%td= link_to I18n.t('.cost.download'), f.object.attachment.url, target: "blank" unless f.object.attachment.blank?
= f.submit(class: 'btn', value: I18n.t('.general.save'))