一个简单的使用group_by的rails 3.1视图 [英] a simple rails 3.1 view using group_by

查看:79
本文介绍了一个简单的使用group_by的rails 3.1视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我正在制作的网站上有一个菜单,它是food_items的菜单。表中的每个记录还包含一个类别字段。这个类别可以是汉堡包,零食,饮料或其他任何他们想要将食物分类的东西。



我试图显示菜单,按这个分类分组领域。我到目前为止的尝试:

  food_item_menus_controller.rb 
...
def food_list
@食物= FoodItem.all
@food_list = @ foods.group_by {| t | t.category}
结束


views / food_item_menus / index.html.haml
= @ food_list.group_by(@ food_items.category).each do | food_list |
%p
= food_list.name
%ul
- for food_items in food_items
%li
= food_items.name

我的错误是我有一个零对象。我想我的电话@foods没有返回任何东西。

  models / food_item.rb 
class FoodItem< ActiveRecord :: Base
has_many:menus,:through => :food_item_menus
has_many:food_item_menus
end

我是否设置了错误的内容?或者,我的想法是错误的,如果我在视图中调用@foods,它实际上并没有调用我在控制器中设置的变量?我越想弄清楚这一点,我越是对一切事情感到困惑,包括基础知识。

您的观点代码充满了错误,它应该是这样的:

  = @ food_list.each do | category,food_items | 
%p
=类别
%ul
- food_items.each do | food |
%li
= food.name

错误细分:




  • @ food_list.group_by(@ food_items.category).each do | food_list |
    - 您还没有为任何地方的 @food_items 赋值; @food_list 被赋值为控制器中 group_by 调用的结果,所以在这里你调用它两次; group_by 会返回一个散列,因此您的每个块应该有两个参数,而不是一个。

  • for food_items in food_items - 为迭代变量和迭代器使用相同的变量名称; food_items 没有在任何地方分配一个值。


I have a menu on a site I am working on, menu of food_items. Each of the records in the table also contains a field of category. This category can be burgers, munchies, drinks, or anything else they want to categorize food items by.

I am trying to display the menu, with items grouped by this category field. My attempt so far:

food_item_menus_controller.rb
...
def food_list
  @foods = FoodItem.all
  @food_list = @foods.group_by { |t| t.category }
end


views/food_item_menus/index.html.haml
= @food_list.group_by (@food_items.category).each do |food_list|
  %p
    = food_list.name
  %ul
    -  for food_items in food_items
      %li
        = food_items.name

My error is that I have a nil object. I guess my call to @foods isn't returning anything.

models/food_item.rb
class FoodItem < ActiveRecord::Base
  has_many :menus, :through => :food_item_menus
  has_many :food_item_menus
end

Did I set up something wrong? Or is my thinking wrong that if I call @foods in the view, it's not actually calling the variable I set in the controller? The more I try to figure this out, the more I am just getting confused about everything, including the basics.

解决方案

Your view code is full of errors, it should be something like this:

= @food_list.each do |category, food_items|
  %p
    = category
  %ul
    - food_items.each do |food|
      %li
        = food.name

Breakdown of errors:

  • @food_list.group_by(@food_items.category).each do |food_list| - you haven't assigned a value to @food_items anywhere; @food_list is assigned as the result from a group_by call in your controller, so here you're calling it twice; group_by returns a hash, so your each block should have two arguments, not one.
  • for food_items in food_items - using the same variable name for the iterated variable and the iterator; food_items is not assigned a value anywhere.

这篇关于一个简单的使用group_by的rails 3.1视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆