Update method in Rails
I need to update a table(MySQL DB) from the JSON received for a different
table controller. I have an "Lists" Controller(for List table in DB) and a
"Table" (for Tables table in DB)Controller. I get the JSON to insert a new
row in Lists table. Now from the JSON received, I need to pick the table
number and update the Tables table too. Below is the JSON recieved
Started POST "/lists.json" for 192.168.1.2 at 2013-08-26 16:55:51 +0530
Processing by ListsController#create as JSON
Parameters: {"list"=>[{"amount"=>"10.50", "orderno"=>"0220130826163623",
"quan
tity"=>"1", "itemname"=>"Patatas Ali Oli", "tableno"=>"02",
"ordstatus"=>"ordere
d"}, {"amount"=>"10.50", "orderno"=>"0220130826163623", "quantity"=>"1",
"itemna
me"=>"Garlic Bread", "tableno"=>"02", "ordstatus"=>"ordered"},
{"amount"=>"12.50
", "orderno"=>"0220130826163623", "quantity"=>"1", "itemname"=>"Entrecote
A La P
lancha", "tableno"=>"02", "ordstatus"=>"ordered"}, {"amount"=>"10.50",
"orderno"
=>"0220130826163623", "quantity"=>"1", "itemname"=>"Pollo Al Horno",
"tableno"=>
"02", "ordstatus"=>"ordered"}]}
From the above JSON i need to pick the "tableno"=>"02" and update my
Tables row for tableno=02. Below is the code I have written in Lists
Controller :
def create
lists = params[:list].collect{|list_attributes|
List.new(list_attributes)}
table = Table.find(:tablabel => List.tableno,:tabstatus => 'reserved');
valid,not_valid = lists.partition{|list| list.valid?}
if not_valid.blank?
lists.map(&:save)
@lists = lists
format.html { redirect_to @list, notice: 'List was successfully
created.' }
format.json { render json: @list, status: :created, location: @list }
else
format.html { render action: "new" }
format.json { render json: @list.errors, status:
:unprocessable_entity }
end
end
Problem is the Lists table is getting updated successfully but extracting
and updating Tables part is not working. I am just a beginner in Rails so
not sure what I am missing. Please advise. Thanks.
No comments:
Post a Comment