我有控制器看起来像:
## controllers/lhgs_controller.rb
def index
@per_page = params[:per_page] || 10
@totalsolds = Totalsold.paginate(:per_page => @per_page, :page => params[:page]).order('date asc').group_by{ |s| [s.date, s.store_id, s.nomor] }
end
观点:
## index.hmtl.erb
<div id="totalsolds"><%= render 'totalsolds' %></div>
## views/_totalsolds.html.erb
<% @totalsolds.each do |(date, store, nomor), totalsold| %>
<tr>
<td><%= date.strftime("%d/%m/%Y") %></td>
<td><%= nomor %></td>
<td><%= Store.find(store).name %></td>
<% totalsold.each do |ts| %>
<td><%= ts.qty == 0 || ts.qty == nil ? "-" :prod.qty %></td>
<% end %>
<td>Rp<%= number_to_currency(totalsold.sum(&:value), :unit => "") %></td>
<td><%= link_to "edit", edit_bt_path(:store_id => store, :date => date, :nomor => nomor), :class => "btn btn-primer" %></td>
</tr>
<% end %>
...
...
<div class="row">
<div class="col-sm-6">
<% line 65 %>
<%= page_entries_info @totalsolds %>
</div>
<div class="col-sm-6">
<%= will_paginate @totalsolds, :style => 'float:right;margin-top:-5px;height:30px;' %>
</div>
</div>
我收到了一个错误:
Lhgs #index
中的ArgumentError显示C:/Sites/cms/app/views/lhgs/_totalsolds.html.erb其中哪一行 65提出:
@lhgs变量似乎是空的。你忘了通过了 will_paginate的集合对象?
我找到了相同的question和solution,但我无法弄清楚如何在我的情况下使用它。
更新:
我找不到任何@lhgs
变量,我尝试将@totalsolds
更改为@lhgs
,现在我的控制器看起来很像:
## controllers/lhgs_controller.rb
def index
@per_page = params[:per_page] || 10
@lhgs = Totalsold.order('date asc').group_by{ |s| [s.date, s.store_id, s.nomor] }
@lhgs = @lhgs.paginate(:per_page => @per_page, :page => params[:page])
end
这是一个错误:
NoMethodError in LhgsController#index
undefined method `paginate' for #<Hash:0x4c034a0>
使用rails console:
irb(main):001:0> @lhgs = Totalsold.order('date asc').group_by{ |s| [s.date, s.store_id, s.nomor] }
.... look at pastebin ......
irb(main):002:0> @lhgs.class
=> Hash
答案 0 :(得分:1)
解决
在控制器上:
def index
@per_page = params[:per_page] || 10
@lhgs = Totalsold.order('date asc').group_by{ |s| [s.date, s.store_id, s.nomor] }
@lhgs_keys = @lhgs.paginate(:per_page => @per_page, :page => params[:page])
end
在视图中:
<% @lhgs_keys.each do |k| %>
<% @lhgs[k].group_by{ |s| [s.date, s.store_id, s.nomor] }.each |(date, store, nomor), totalsold| %>
.....
....
<% end %>
<% end %>
<div class="row">
<div class="col-sm-6">
<% line 65 %>
<%= page_entries_info @lhgs_keys %>
</div>
<div class="col-sm-6">
<%= will_paginate @lhgs_keys, :style => 'float:right;margin-top:-5px;height:30px;' %>
</div>
</div>
答案 1 :(得分:0)
试试这样:
# controllers/lhgs_controller.rb
def index
@per_page = params[:per_page] || 10
@totalsolds = Totalsold.paginate(:per_page => @per_page, :page => params[:page]).order('date asc').group_by{ |s| [s.date, s.store_id, s.nomor] }
respond_to do |format|
format.html
end
end
index.html可以是这样的:
<% @totalsolds.each do |(date, store, nomor), totalsold| %>
<tr>
<td><%= date.strftime("%d/%m/%Y") %></td>
<td><%= nomor %></td>
<td><%= Store.find(store).name %></td>
<% totalsold.each do |ts| %>
<td><%= ts.qty == 0 || ts.qty == nil ? "-" :prod.qty %></td>
<% end %>
<td>Rp<%= number_to_currency(totalsold.sum(&:value), :unit => "") %></td>
<td><%= link_to "edit", edit_bt_path(:store_id => store, :date => date, :nomor => nomor), :class => "btn btn-primer" %></td>
</tr>
<% end %>
...
<div class="row">
<div class="col-sm-6">
<% line 65 %>
<%= page_entries_info @totalsolds %>
</div>
<div class="col-sm-6">
<%= will_paginate @totalsolds, :style => 'float:right;margin-top:-5px;height:30px;' %>
</div>
</div>