https://gist.github.com/1114858.
How can I set the quantity DeliveryItem in the same form so that when
I update the customer attributes the Delivery is created with products
and their quantity?
On 30 July 2011 21:22, Mauro [email protected] wrote:
https://gist.github.com/1114858.
How can I set the quantity DeliveryItem in the same form so that when
I update the customer attributes the Delivery is created with products
and their quantity?
no solution?
On Sun, Jul 31, 2011 at 1:47 PM, Conrad T. [email protected]
wrote:
Or do you have tests which might add some clarity to what you’re trying
to
achieve?
On Sun, Jul 31, 2011 at 11:12 AM, Mauro [email protected] wrote:
On 30 July 2011 21:22, Mauro [email protected] wrote:
https://gist.github.com/1114858.
How can I set the quantity DeliveryItem in the same form so that when
I update the customer attributes the Delivery is created with products
and their quantity?no solution?
Mauro, I’m not 100% sure what you’re trying to ask here. Thus, could
you
explain
the steps starting with how the user would interact with the form to
what you would
like to happen? For example,
a) The user navigates to the page containing the form.
b) The user does X
c) …
-Conrad
On 31 July 2011 22:52, Conrad T. [email protected] wrote:
c) …
Ok, sorry but my english is really bad…
Then, as you see in the code the Customer has many deliveries and one
Delivery has many products through delivery_items.
I want to do this: I call the show action for Customer;
I view customer details and a form to create a delivery for that
customer.
The delivery has one or more products.
When I submit the form it creates the delivery for the customer with
one or more delivery_items, that is the join model.
The delivery_item contains the product but the quantity attribute is
nil, I want to populate the quantity when the delivery was created so
that the delivery has products with their quantity.
On 1 August 2011 08:22, Chris K. [email protected] wrote:
Add an accepts_nested_attributes_forcall 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.
I think your problem is in the way you’ve used the AR relationships in
constructing the form you have there. Your form is using the
has-many-through relationship from Delivery to Product to construct the
form. This was all right before you had anything meaningful at the
DeliveryItem level, but now the whole form needs to be reworked to stop
thinking of Delivery as a bunch of Products and instead as a number of
DeliveryItems, each of which is associated with a Product.
I haven’t had to do anything like this before, but I would try to solve
it
as follows:
- Add an accepts_nested_attributes_for call to Delivery for
:delivery_items. - Add a quantity attribute to DeliveryItem
- 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.
On 1 August 2011 12:20, Chris K. [email protected] wrote:
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.build3.times { @delivery.delivery_items.build }
why 3.times?
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 1 August 2011 13:35, Chris K. [email protected] wrote:
No reason. It can be whatever number of times you decide. If you want to
show the user a form with 10 delivery items, then it would be 10.times.
That’s difficult because is the user that decide how much products
deliver and then how much delivery_items contains a delivery.
Well then you need to go with an alternative that allows the user to add
rows to the form using some dynamic HTML. I’d get it working with a
static
version as I’ve suggested first and then try to adapt it to this
requirement.
On Aug 1, 2011, at 9:05 AM, Chris K. wrote:
10.times.
That’s difficult because is the user that decide how much products
deliver and then how much delivery_items contains a delivery.
Work through the Railscast on accepts_nested_attributes (Complex Forms
or something like that, don’t recall the title exactly – it’s a two-
parter) because Ryan covers this case exactly. And he follows the same
path of “make it work as a static form first” that you should try to
follow as a rule whenever making Ajax form magick happen.
Walter
No reason. It can be whatever number of times you decide. If you want
to
show the user a form with 10 delivery items, then it would be 10.times.