salve a tutt,
sto provando a modificare l’esempio visibile su:
sintesi: un unico form di creazione di un modello A (nell’esempio:
project) che ha molti modelli B (nell’esempio: tasks), gestito con la
minore quantità di codice possibile.
nel mio caso, semplicemente, il modello B ha non un attributo, ma due.
nel controller, il metodo new:
def new
@attachment = Attachment.new
2.times { @attachment.amounts.build }
end
questo è quello che scrivo nel template:
- form_for :attachment, @attachment, :url => {:action => :create},
:html => {:multipart => true} do |f|
.field
= f.label :title.l, {}, {:class => “required”}
%br
= f.text_field :title
… - for amount in @attachment.amounts
- fields_for “attachment[amounts_attributes][]”, amount do |af|
.field
= af.label :tax_base, :tax_base.l
%br
= af.text_field :tax_base
.field
= af.label :vat_rate, :vat_rate.l
%br
= af.text_field :vat_rate, :size => 2
…
- fields_for “attachment[amounts_attributes][]”, amount do |af|
(traduco per chi non usa haml (ma davvero? ma che aspettate?)):
<%- form_for :attachment, @attachment, :url => {:action => :create,
:controller => :attachment}, :html => {:multipart => true} do |f| -%>
<%= f.text_field :title -%>
<%= af.text_field :tax_base -%>
<%= af.text_field :vat_rate, :size => 2 -%>
il problema è che il controller riceve, in params:
{“attachment”=>{
“title”=>“”,
“amounts_attributes”=>[
{“tax_base”=>“”},
{“vat_rate”=>“”, “tax_base”=>“”},
{“vat_rate”=>“”}]}
}
invece, vorrei che ricevesse:
"amounts_attributes"=>[{"tax_base"=>"", "vat_rate"=>""},
{"vat_rate"=>"", "tax_base"=>""}]}
N.B.: ho provato con IE7, Firefox 3, Safari for Windows, Chrome.
cosa mi sfugge, esattamente?
grazie
pietro