i am trying to implement a forum page using the tutorial in this page!. Here Forum is a model. This is the controller code:
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
the error is :
undefined method `all' for Forum:Module
Here is the forum model ( 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
How can i rectify this error? I am new to ROR and unable to find a proper solution for this error.
The error above is saying there is no method defined for the Module Forum. However, the definition of Forum clearly shows that's a class, not a module.
The only explanation is that you have another definition of Forum somewhere in your application, where you define it as a Module, that is loaded before the model and it conflicts with your application.
Be very careful you didn't call your application Forum, otherwise the main application namespace will conflict with your model (there is a high chance that's the problem).
In this case, you either rename your application or (easier) the model. In fact, the application namespace is defined as module.
Search the source code of your application for a Forum module definition and remove it. It may also be in a gem (very unlikely, but not impossible) so make sure you know the source code of the dependencies your are using.
This could be something to do with your routes.
Try in config/routes.rb
root :to => 'forums#index'
instead of
map.root :controller => 'forums'
It's a rails 2/3 thing and I think this tutorial is written in 2.
If you are trying to learn Rails, I recommend Michael Hartl's Rails Tutorial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With