Forum: Ruby on Rails Controller Modules Messed Me Up

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Chad C. (Guest)
on 2006-06-10 02:38
Modules are exactly what I needed to figure out, but once I started my
app over again putting my controller under a module folder, I can't get
my basic app going.

Basically I have a News app, where we will post PR and other articles of
interest.  It was working fine with a model/controller combo of
news/index for the public and news/admin for the administrators.  Keep
in mind this was the very early stages of development.

So I knew I wanted more components in the admin area, so I finally found
modules and went back to do it all again with news/index for the public
and admin/news/index for the administrators.  Everything set up fine
with the generator and scaffold for adding news in the admin area.
However now I can't get the public area to show the list of news.  Here
is what I did:


class NewsController < ApplicationController

  def Index
    @news = News.news_items


class News < ActiveRecord::Base

  validates_presence_of :date_added, :date_live, :news_date, :added_by,
:headline, :summary, :article

  def self.news_items
      find( :all,
            :conditions   => "news_date <= now()",
            :order        => "news_date desc")



<h1>Recent News</h1>

<% for news in @news %>

<h3><%= news.headline %></h3>

<% end %>

Here is the output of http://localhost:3000/news

NoMethodError in News#index

Showing app/views/news/index.rhtml where line #3 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.each
Extracted source (around line #3):

1: <h1>Recent News</h1>
3: <% for news in @news %>
5: <h3><%= news.headline %></h3>
RAILS_ROOT: /Users/Chad/Sites/xxx/public/../config/..

Application Trace | Framework Trace | Full Trace
#{RAILS_ROOT}/app/views/news/index.rhtml:3:in `_run_rhtml_news_index'

Parameters: None

Show session dump


Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}

It appears to me that @news doesn't contain anything according to the
error.  Where is my problem in the code?  This worked fine when the
admin section was news/admin, so is there something different that must
be done when it is in the module /admin/news/admin?
This topic is locked and can not be replied to.