NoMethodError in partial driving me mad

Hello,

i simplified have a news model, a news category model and a news
controller.
my _news.rhtml partial renders a single news entry.

in my controller there are the actions show and show_category.
if the "show" action is called, a single news item is rendered through
my partial with no errors.

if the "show_category" action is called, i get a NoMethodError while
displaying my news through the partial.

news_controller.rb:

i tried this but it did not work.

here is the new show_category action:

def show_category
    @newses = NewsCategory.find(params[:id]).news
    render_partial 'news', :collection => @newses
  end

the new error is:

NoMethodError in News#show_category

Showing app/views/news/_news.rhtml where line #2 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.title

Extracted source (around line #2):

1: <div>
2: <h1><%=h news.title %> - Kategory: <%= news.news_category.title
%></h1>
3: <p>
4: <%= bbcodeize news.content %>
5: </p>

still no one hast a answer for this problem?
im stuck till now.. :frowning:

to sum up, first error was a noMethodError for
News:Class

second one a nil object...

help! :frowning:

have you tried testing the News class and the NewsCategory class
associations? I see a has_many association in the first full trace..
Can you test if it the news_category is working correctly?

Pascal,

You were almost there originally... you just need to pass the first
call to render a locals hash so that the news variable in the partial
is populated correctly. The collection render does it automatically.
BTW, your problem does not appear to have anything to do with Rails
pluralization.

def show
   @news = News.find(params[:id])
   render :partial => 'news', :locals => { :news => @news }
end

def show_category
   @news = NewsCategory.find(params[:id]).news
   render :partial => 'news', :collection => @news
end

Cheers,
Obie

oh my god, this is it! it works.

did not realy understand what the News:Class came from but… its gone now and iam happy :slight_smile:
i’ll try to understand this later after finishing (finally) my tiny app.

tanks

Pascal