Rails - Group_by方法不起作用

时间:2018-03-21 10:37:51

标签: ruby-on-rails

我正在尝试按月在rails和SQL3中对项目(游戏)进行分组,但它会生成:

错误

3月17日
<% @game_months.each do |month, games| %>
    <% for game in games %>
    <a href="<%= game_path(game)%>" class="col-md-4 m-t-md">
   <div class="box b-a m-a" style="box-shadow: 0px 4px 2px 0px rgba(0,0,0,0.02);">

这是我的游戏控制器

class GamesController < ApplicationController



def index
    @game = current_user.games
    @games = Game.all.order("created_at DESC")
    @game_months = @games.group_by {|g| g.created_at.beginning_of_month }
end

我的观点

<% @game_months.each do |month, games| %>
        <% for game in games %>
        <a href="<%= game_path(game)%>" class="col-md-4 m-t-md">
    <div class="box b-a m-a" style="box-shadow: 0px 4px 2px 0px rgba(0,0,0,0.02);">
      <div class="item b-b">
                <div><%= image_tag game.photos[0].image.url(:medium), class:"img-responsive" if game.photos.length > 0 %></div>
      </div>
      <div class="p-a text-left white">
        <h6 class="text-md text-black block p-b-sm"><strong><%= game.game_name %></strong></h6>
        <span class="text-muted p-t-md"><%= game.game_description %></span>
        <div class="m-t-md">
                <span><%= image_tag avatar_url(game.user), class:"w-24 circle b-a" %></span>
        <span class="m-t-sm text-black pull-right text-md"></span>
        </div>
      </div>
    </div>
  </a>
    <% end %>
<% end %>

我不知道自己做得不好。请帮忙,我不知道该怎么做才能解决这个问题!

干杯! :)

1 个答案:

答案 0 :(得分:0)

更改

object(SimpleXMLElement)#1 (2) {
  ["sns"]=>
  object(SimpleXMLElement)#2 (1) {
    ["@attributes"]=>
    array(8) {
      ["id"]=>
      string(1) "1"
      ["name"]=>
      string(8) "Senzor A"
      ["type"]=>
      string(1) "1"
      ["status"]=>
      string(1) "0"
      ["unit"]=>
      string(1) "0"
      ["val"]=>
      string(3) "4.5"
      ["w-min"]=>
      string(0) ""
      ["w-max"]=>
      string(0) ""
    }
  }
  ["status"]=>
  object(SimpleXMLElement)#3 (1) {
    ["@attributes"]=>
    array(3) {
      ["level"]=>
      string(1) "2"
      ["location"]=>
      string(3) "AAA"
      ["time"]=>
      string(19) "03/21/2018 14:09:08"
    }
  }
}

@games = Game.all.order("created_at DESC")
@game_months = @games.group_by {|g| g.created_at.beginning_of_month }

它应该产生如下所示的哈希:

@game_months = Game.all.group_by { |g| g.created_at.month }

现在迭代哈希并执行您想要执行的操作。