从我的 Android 应用程序接收到数据后,我需要立即更新视图(HTML)。我以 JSON 格式接收数据,然后将其插入表(MySQL)中。通过这种插入,我还想在具有 HTML 视图的终端上发送接收数据。下面是我收到的 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。现在我需要更新另一个视图,从上面的 JSON 中提取项目名称和数量。下面是我正在更新数据库并尝试调用另一个视图的列表中的控制器代码
def create
lists = params[:list].collect{|list_attributes| List.new(list_attributes)}
Table.where(:tablabel => "Table"+lists.first.tableno).update_all(:tabstatus => 'reserved')
orders = List.select("itemname,SUM(quantity) as quantity").group("itemname"))
render :partial => 'kitchen/index', :collection => orders
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
我正在尝试解析必填字段并通过以下代码调用另一个视图
orders = List.select("itemname,SUM(quantity) as quantity").group("itemname"))
render :partial => 'summary/index', :collection => orders
这summary/index
是另一个控制器名称,我正在尝试传递orders
给它。在我的 index.html.erb 文件中,我编写了以下代码;
<% @orders.each do |order| %>
<div class="item-name"><%= order.itemname %></div>
<div class="qty"><%= order.quantity %></div>
<% end %>
当我运行它时,我收到“评估 nil.each 时发生错误”错误。好像我没有正确传递订单对象。
我是 Rails 的初学者,所以不确定我是否遗漏了什么。尝试了通过网络搜索找到的更多选项,但没有运气。请指教。谢谢。