Skip to content

Instantly share code, notes, and snippets.

@RemiBa
Created January 2, 2013 09:31
Show Gist options
  • Save RemiBa/4433276 to your computer and use it in GitHub Desktop.
Save RemiBa/4433276 to your computer and use it in GitHub Desktop.
<% if orderitem.alreadyExists? %>
<%= form_for [@order, orderitem] do |f| %>
<%= f.label :quantity %>
<%= f.number_field :quantity, :min => 1, :value => alreadyexists.quantity.to_i %>
<%= f.hidden_field :product_id, :value => orderitem.product.id %>
<%= f.submit 'Change' %>
<% end %>
<% else %>
<% item = Orderitem.new(:order => @current_bom, :product => product, :quantity => 0) %>
<%= form_for [@order, item] do |f| %>
<%= f.label :quantity %>
<%= f.number_field :quantity, :min => 1 %>
<%= f.hidden_field :product_id, :value => item.product.id %>
<%= f.submit 'Add' %>
<% end %>
<% end %>
Before update_attribute:
+------------+-------------------+------------+
+ order_id + product_id + quantity +
+------------+-------------------+------------+
+ 1 + C00070033000003 + 150 +
+------------+-------------------+------------+
+ 1 + C00080033000003 + 47 +
+------------+-------------------+------------+
After update_attribute:
+------------+-------------------+------------+
+ order_id + product_id + quantity +
+------------+-------------------+------------+
+ 1 + C00070033000003 + 120 +
+------------+-------------------+------------+
+ 1 + C00080033000003 + 120 +
+------------+-------------------+------------+
class OrderitemsController < ApplicationController
def update
orderitem = Orderitem.find_by_order_id_and_product_id(params[:order_id], params[:orderitem][:product_id])
if orderitem
if orderitem.update_attribute(:quantity, params[:orderitem][:quantity].to_i)
flash[:notice] = "Saved!"
else
flash[:notice] = "Error updating your bill of materials"
end
else
flash[:notice] = 'Error updating your bill of materials'
end
redirect_to(:back)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment