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:
/app/controllers/news_controller.rb
class NewsController < ApplicationController
def Index
@news = News.news_items
end
end
/app/models/news.rb
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")
end
end
/app/views/news/index.rhtml
<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>
2:
3: <% for news in @news %>
4:
5: <h3><%= news.headline %></h3>
6:
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''
Request
Parameters: None
Show session dump
Response
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?
--
Posted via http://www.ruby-forum.com/.