我需要从为不同的表控制器接收到的 JSON 更新表(MySQL DB)。我有一个“Lists”控制器(用于 DB 中的 List 表)和一个“Table”(用于 DB 中的 Tables 表)控制器。我让 JSON 在 Lists 表中插入新行。现在从收到的 JSON 中,我需要选择表号并更新 Tables 表。以下是收到的 JSON
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"}]}
从上面的 JSON 中,我需要"tableno"=>"02"
为 tableno=02 选择并更新我的表行。下面是我在 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
问题是列表表已成功更新,但提取和更新表部分不起作用。我只是 Rails 的初学者,所以不确定我错过了什么。请指教。谢谢。