rails - undefined方法全部用于论坛:模块

tri*_*ror 2 ruby ruby-on-rails ruby-on-rails-3

我正在尝试使用此页面中的教程实现论坛页面!这里论坛是一个模型.这是控制器代码:

class ForumsController < ApplicationController
  before_filter :admin_required, :except => [:index, :show]

  def index
    @forums = Forum.all
  end

  def show
    @forum = Forum.find(params[:id])
  end

  def new
    @forum = Forum.new
  end

  def create
    @forum = Forum.new(params[:forum])
    if @forum.save
      redirect_to @forum, :notice => "Successfully created forum."
    else
      render :action => 'new'
    end
  end

  def edit
    @forum = Forum.find(params[:id])
  end

  def update
    @forum = Forum.find(params[:id])
    if @forum.update_attributes(params[:forum])
      redirect_to @forum, :notice  => "Successfully updated forum."
    else
      render :action => 'edit'
    end
  end

  def destroy
    @forum = Forum.find(params[:id])
    @forum.destroy
    redirect_to forums_url, :notice => "Successfully destroyed forum."
  end
end
Run Code Online (Sandbox Code Playgroud)

错误是:

undefined method `all' for Forum:Module
Run Code Online (Sandbox Code Playgroud)

这是论坛模型(models/forum.rb):

class Forum < ActiveRecord::Base
  attr_accessible :name, :description
  has_many :topics, :dependent => :destroy

  #method to find the most recent forum topics
  def most_recent_post  
  topic = Topic.first(:order => 'last_post_at DESC', :conditions => ['forum_id = ?', self.id])  
  return topic  
end  
end
Run Code Online (Sandbox Code Playgroud)

我该如何纠正这个错误?我是ROR的新手,无法为此错误找到合适的解决方案.

Sim*_*tti 5

上面的错误是说没有定义的方法Module Forum.但是,清晰度的定义Forum清楚地表明这是一个类,而不是一个模块.

唯一的解释是,您在应用程序中的某个地方有另一个论坛定义,您将其定义为a Module,在模型之前加载并与应用程序冲突.

非常小心你没有调用你的应用程序Forum,否则主应用程序命名空间将与你的模型冲突(很可能是问题).在这种情况下,您可以重命名应用程序或(更简单)模型.实际上,应用程序命名空间被定义为模块.

在应用程序的源代码中搜索论坛模块定义并将其删除.它也可能在gem中(非常不可能,但并非不可能),因此请确保您知道正在使用的依赖项的源代码.