[ruby-it] rails: form complessi?
Pietro Giorgianni
giorgian a gmail.com
Ven 19 Set 2008 16:33:08 CEST
salve a tutt,
sto provando a modificare l'esempio visibile su:
http://railscasts.com/episodes/73-complex-forms-part-1
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
...
(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| -%>
<div class="field">
<%= f.label :title.l, {}, {:class => "required"} -%> <br/>
<%= f.text_field :title -%>
</div>
...
<%- for amount in @attachment.amounts -%>
<%- fields_for "attachment[amounts_attributes][]", amount do |af| -%>
<div class="fields">
<%= af.label :tax_base, :tax_base.l -%> <br/>
<%= af.text_field :tax_base -%>
</div>
<div class="fields">
<%= af.label :vat_rate, :vat_rate.l -%> <br/>
<%= af.text_field :vat_rate, :size => 2 -%>
</div>
<%- end -%>
<%- end -%>
...
<%- end -%>
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
More information about the Ml
mailing list