# Rispettare le convenzioni

**URL:** <https://www.ruby-forum.com/t/rispettare-le-convenzioni/203099>\
**Category:** IT\
**Created:** [February 12, 2011, 8:18pm UTC](https://www.ruby-forum.com/t/rispettare-le-convenzioni/203099 "2011-02-12T20:18:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 12, 2011, 8:18pm UTC](https://www.ruby-forum.com/t/rispettare-le-convenzioni/203099/1 "2011-02-12T20:18:03Z")

</div>

Spesso e’ complicato a meno che, come al solito e come e’ sicuramente  
probabile si tratti di impostazione sbagliata.  
Modelli:

Customer  
has\_many :deliveries  
has\_one :document

Delivery  
belongs\_to :customer  
belongs\_to :operator  
has\_many :delivery\_items  
has\_many :products, :through =\> :delivery\_items

Document  
belongs\_to :customer

Product  
has\_many :delivery\_items  
has\_many :deliveries, :through =\> :delivery\_items

L’utente che utilizza l’applicazione deve:  
fare una ricerca del Customer, una volta trovato deve poter  
visualizzare i dettagli del customer e, tramite un form, associargli  
un Delivery e un Document, contemporaneamente al Delivery deve essere  
associati uno o piu’ Products.  
Quello che ho pensato di fare e’ visualizzare il form nella view show  
di Customer:

CustomersController  
def show  
@customer = Customer.find(params[:id])  
@delivery = Delivery.new  
@document = Document.new  
@products = Product.all  
end

show.html.erb

**\<%= Customer.human\_attribute\_name("full\_name") %\>:** \<%= @customer.full\_name %\>

. . . \<%= render 'deliveries/form' %\>

il partial e’:  
\<%= simple\_form\_for([@customer, @delivery]) do |f| %\>

\<%= f.input :delivered\_at, :as =\> :hidden, :input\_html =\> {  
:value =\> Date.today } %\>

  

\<% for product in @products %\>  
\<%= check\_box\_tag ‘delivery[product\_ids][]’, product.id,  
@delivery.products.include?(product) %\>  
\<%= product.description %\>  
  
\<% end %\>  
\<%= f.error :products%\>

  
  
\<%= simple\_fields\_for @document do |doc| %\>  

\<%= doc.label :doc\_type %\>:  
  
\<%= doc.text\_field :doc\_type %\>  
\<%= doc.error :doc\_type %\>

  

\<%= doc.label :doc\_number %\>:  
  
\<%= doc.text\_field :doc\_number %\>  
\<%= doc.error :doc\_number %\>

  

\<%= doc.label :issued\_by %\>:  
  
\<%= doc.text\_field :issued\_by %\>  
\<%= doc.error :issued\_by %\>

  
\<% end %\>

Nel routes.rb ho:

resources :customers do  
resources :deliveries  
end

Tutto funziona ma quello che non mi sfagiola e’ la creazione degli  
oggetti @delivery e @document dentro l’action show che per definizione  
dovrebbe essere usata per mostrare non per creare.  
Non trovo pero’ altre soluzioni per far funzionare il form di cui sopra.
