Ruby on Rails Monday, August 1, 2011

Never used simple_form, but I'd try it like this.  Forgive me if the syntax isn't quite right.

controller:
  
def show
  @customer = Customer.find(params[:id])
  @delivery = @customer.deliveries.build
3.times { @delivery.delivery_items.build }
  @products = Product.all
  @products_selected = []
  Settings.default_delivery.products.each{|product| @products_selected << product.id_value}

form:
= simple_form_for @customer do |f|
  = f.simple_fields_for :deliver do |deliveries_f|
= deliveries_f.input :notes
= f.simple_fields_for :delivery_items do |delivery_items_f|
    = delivery_items_f.association :product, :multiple => false, :selected => @products_selected,
                                :input_html => { :title => "- Select product -" }, :label => false
= delivery_items_f.input :quantity

Essentially, you're using the same technique of building the DeliveryItems under the Delivery just as you built the Delivery under the Customer.  With this technique, you're limited to defining a fixed number of DeliveryItems before the form is produced, but adding a dynamic element that would add more rows if the user needs them should be trivial.

HTH

On Mon, Aug 1, 2011 at 10:41 AM, Mauro <mrsanna1@gmail.com> wrote:
On 1 August 2011 08:22, Chris Kottom <chris@chriskottom.com> wrote:

> Add an accepts_nested_attributes_for call to Delivery for :delivery_items.

done

> Add a quantity attribute to DeliveryItem

already done

> Change the form from a single select with a list of Products to a number of
> rows, each with a single-item select for Product and a text field for
> entering quantity.

But DeliveryItem belongs_to :product so to add, say three products, I
have to create three delivery_items submitting the form three times.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment