2013-10-21 38 views
2

我正在嘗試使用本教程page來實現論壇頁面。這裏論壇是一個模型。這是控制器代碼:rails - 未定義的方法全部用於論壇:模塊

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 

的錯誤是:

undefined method `all' for Forum:Module 

下面是論壇車型(型號/ 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 

我怎樣才能糾正這個錯誤?我是ROR新手,無法找到適合此錯誤的解決方案。

+1

請提供您的論壇模型的源代碼 –

+1

重做第4步。論壇不是模塊,而是應用/模型中的模型 – TheIrishGuy

+0

@ThelrishGuy對於問題中的錯誤感到抱歉,我按照指定的方式完成了它。 – trialError

回答

1

這可能與您的路線有關。

嘗試config/routes.rb

root :to => 'forums#index'

,而不是

map.root :controller => 'forums'

這是一個軌道2/3的事情,我覺得這個教程是寫在2

如果您正在嘗試學習Rails,我推薦Michael Hartl's Rails Tutorial

+1

嘿我以前做過這種改變,但仍然得到相同的錯誤 – trialError

+0

是的,我認爲西蒙娜釘了它。很可能已經定義了論壇模塊。 –

5

上面的錯誤是說沒有爲ModuleForum定義的方法。但是,Forum的定義清楚地表明這是一個類,而不是一個模塊。

唯一的解釋是你在你的應用程序的某個地方有另一個論壇的定義,你定義它爲Module,它在模型之前加載並且與你的應用程序衝突。

要非常小心,你沒有打電話給你的應用程序Forum,否則主應用程序的命名空間將與您的模型衝突(有一個高的機會這就是問題所在)。 在這種情況下,您可以重命名應用程序或(更簡單)模型。實際上,應用程序名稱空間被定義爲模塊。

搜索您的應用程序的源代碼的論壇模塊定義並將其刪除。它也可能在一個寶石(很不可能,但不是不可能),所以確保你知道你正在使用的依賴關係的源代碼。

相關問題