【问题标题】:uninitialized constant ActsAsTaggableOn未初始化的常量 ActsAsTaggableOn
【发布时间】:2015-06-24 23:13:00
【问题描述】:

我正在尝试在我的应用中创建一个菜单,为我的 Places 模型的每个类别显示一个链接。

我做到了,但仅在我第一次访问我的应用程序时有效,之后我收到此错误

应用程序中的名称错误#index

未初始化的常量 ActsAsTaggableOn::Category

Extracted source (around line #9):
7
8
9
10
11
12

    @places = Place.all
    @states = State.all
    @categories = ActsAsTaggableOn::Category.all


  end

如果我重新启动我的 Rails 服务器,我的菜单会在第一次运行时再次出现错误。

我的应用程序控制器

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  def index
    @places = Place.all
    @states = State.all
    @categories = ActsAsTaggableOn::Category.all
  end
end

我的地点

class Place < ActiveRecord::Base
  validates_presence_of :name , :state, :city, :neighborhood, :adress,:latitude, :longitude
  belongs_to :user

  acts_as_ordered_taggable_on :categories, :obstacles
end

Index.erb 菜单代码

    <% categories.each do |category| %>  

    <li><a href="#"><i class="fa fa-cutlery"></i><%= category.name%>      <span>32</span></a></li>

   <%end %>

【问题讨论】:

  • 请不要编辑您问题的基本前提。另外,不要编辑问题的标题,请accept an answer 这对您有帮助。

标签: ruby-on-rails acts-as-taggable-on


【解决方案1】:

我认为你需要关注而不是@categories = ActsAsTaggableOn::Category.all

    @categories = @places.map(&:categories)

【讨论】:

  • 天哪,我太笨了哈哈,@categories = Category.all 一切顺利 ^^,谢谢你的帮助
  • @user1684409:接受答案,这样这个问题就结束了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多