Ruby on Rails - Editing multiple rows of data on one form
I think I may have poorly chosen my first application to learn I am trying to set up some simple profit-loss reports for our store Natures Paws.
Working with a legacy schema with
So, if a view has some code, iterating through some line items (each a row in a database table), like so
<% for @line in @profit_report.report_lines %>
<%= h @line.product_name %>
<%= text_field("line[]", 'wholesale_price', :size => 4, :maxsize => 4, :onchange => 'updateWholeSaleTotal(this.value);') %>
<%= text_field("line[]", 'ourshipping_price', :size => 4, :maxsize => 4) %>
<%= h @line.customer_price %>
<%= h @line.customer_shipping_price %>
<%= text_field("line[]", 'usps_price', :size => 4, :maxsize => 4) %>
<%= h @line.commission_paid %>
<% end %>
Then the Controller code can then update each lines in the model like so...
@params[:line].each do |idx, line|
report_line = ReportLine.find(idx)
report_line.update_attributes(line)
end
I have to say that is pretty sweet.
No comments:
Post a Comment