在Rails中更新视图内容

时间:2013-08-27 07:01:24

标签: ruby-on-rails ruby-on-rails-3.2

我需要在从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"}]}

然后我更新在我的数据库表“lists”中收到的这个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时发生错误”错误。好像我没有正确传递订单对象。

我是铁轨的初学者,所以不确定我是否遗漏了任何东西。尝试通过网络搜索找到的更多选项,但没有运气。请指教。感谢。

0 个答案:

没有答案