Hey all.
I’m trying to copy this example I found, which I’m assuming no longer
works… that or I’m stupid… I want to have the position for each
“row” show up ina box, with a “update position” button that submits
the form, and updates the position… in retropect I shuld have used
the Draggable effect in Scriptaculous, it would have been faster.
Anyway, now I’m wondering how I would do this… Any help would be
great.
I get a parameter that holds everything I want
{“commit”=>“Update Positions”,
“rate_group_additional_rate”=>{“1488”=>{“position”=>“6”},
“948”=>{“position”=>“1”}, “949”=>{“position”=>“2”},
“1396”=>{“position”=>“5”}, “950”=>{“position”=>“3”},
“951”=>{“position”=>“4”}}}
where rate_group_additional_rate is the model’s position I want to
upgrade.
http://lists.rubyonrails.org/pipermail/rails/2005-February/002489.html
class PurchaseOrdersController < ApplicationController
def new
@purchase_order = PurchaseOrder.new(@params[‘purchase_order’])
@order_items = @purchase_order.items
end
end
Then, in your view (new.rhtml, for example):
<% @order_items.each do |@order_item| %>
<%= text_field “order_item[]”, “name” %>
<%= text_field “order_item[]”, “price” %>
… etc …
<% end %>
The trailing square brackets after the object name (order_item[])
tells Rails to automatically add the index (“id” field) of that
order_item to the form element (a text field in this case). So you’ll
get HTML like this, for example:
<input type=“text” name=“order_item[33][name]” … >
<input type=“text” name=“order_item[33][price]” … >
<input type=“text” name=“order_item[39][name]” … >
<input type=“text” name=“order_item[39][price]” … >
This is especially handy for the new 0.9.5 “update” method which will
take a hash of hashes and update the correct IDs:
def create
OrderItem.update @params[‘order_item’] #Updates both items 33 and
39
end
The first part works great, I can’t seem to get my head around how to
update all the “Order Items” in this case. When I type the exmple as
it is below, I get a “wrong number of arguments (1 for 2)” error.