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:

def show
@news = News.find(params[:id])
render_partial ‘news’
end

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

_news.rhtml:

<%=h news.title %> - Kategory: <%= news.news_category.title %>

<%= bbcodeize news.content %>

erstellt von <%=link_to news.user.login, :controller => "user", :action => "show", :id => news.user.id %> vor <%= time_ago_in_words news.created_at %>. <%= "zuletzt geändert vor " + time_ago_in_words(news.updated_at) unless news.updated_at.nil? %>

ERROR:

NoMethodError in News#show_category

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

undefined method `title’ for News:Class

Extracted source (around line #2):

1:


2:

<%=h news.title %> - Kategory: <%= news.news_category.title
%>


3:


4: <%= bbcodeize news.content %>
5:

RAILS_ROOT: script/…/config/…
Application Trace | Framework Trace | Full Trace

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
base.rb:1235:in method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ associations/has_many_association.rb:111:insend’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
associations/has_many_association.rb:111:in method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:946:inwith_scope’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
associations/has_many_association.rb:103:in method_missing' #{RAILS_ROOT}/app/views/news/_news.rhtml:2:in_run_rhtml_47app47views47news47_news46rhtml’
app/controllers/news_controller.rb:56:in show_category' -e:4:inload’
-e:4

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
base.rb:1235:in method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ associations/has_many_association.rb:111:insend’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
associations/has_many_association.rb:111:in method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:946:inwith_scope’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
associations/has_many_association.rb:103:in method_missing' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb: 326:insend’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:
326:in compile_and_render_template' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb: 301:inrender_template’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:
260:in render_file' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb: 275:inrender’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/
partials.rb:59:in render_partial' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ benchmarking.rb:26:inbenchmark’
/usr/lib/ruby/1.8/benchmark.rb:293:in measure' /usr/lib/ruby/1.8/benchmark.rb:307:inrealtime’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
benchmarking.rb:26:in benchmark' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/ partials.rb:58:inrender_partial’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:850:in render_partial' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:1095:insend’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:1095:in perform_action_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:632:incall_filter’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
filters.rb:634:in call_filter' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:619:inperform_action_without_benchmark’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
benchmarking.rb:66:in perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
benchmarking.rb:66:in perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ rescue.rb:83:inperform_action’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:430:in send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:430:inprocess_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
filters.rb:624:in process_without_session_management_support' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ session_management.rb:114:inprocess’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:330:in process' /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:indispatch’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:78:in
process' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:insynchronize’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:in
process' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:618:inprocess_client’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:in each' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:inprocess_client’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in run' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:ininitialize’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in new' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in
initialize' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:innew’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in run' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb: 271:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:
270:in each' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb: 270:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:127:in
run' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/command.rb: 211:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:243
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:488:in load' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:488:inload’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:342:in new_constants_in' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:488:inload’
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/
mongrel.rb:60
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:495:in require' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:342:innew_constants_in’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:495:in require' /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39 /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’
script/server:3

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
base.rb:1235:in method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ associations/has_many_association.rb:111:insend’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
associations/has_many_association.rb:111:in method_missing' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:946:inwith_scope’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
associations/has_many_association.rb:103:in method_missing' #{RAILS_ROOT}/app/views/news/_news.rhtml:2:in_run_rhtml_47app47views47news47_news46rhtml’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:
326:in send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb: 326:incompile_and_render_template’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:
301:in render_template' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb: 260:inrender_file’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:
275:in render' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/ partials.rb:59:inrender_partial’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
benchmarking.rb:26:in benchmark' /usr/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/usr/lib/ruby/1.8/benchmark.rb:307:in realtime' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ benchmarking.rb:26:inbenchmark’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/
partials.rb:58:in render_partial' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:850:inrender_partial’
app/controllers/news_controller.rb:56:in show_category' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:1095:insend’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:1095:in perform_action_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:632:incall_filter’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
filters.rb:634:in call_filter' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:619:inperform_action_without_benchmark’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
benchmarking.rb:66:in perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
benchmarking.rb:66:in perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ rescue.rb:83:inperform_action’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:430:in send' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:430:inprocess_without_filters’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
filters.rb:624:in process_without_session_management_support' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ session_management.rb:114:inprocess’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:330:in process' /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:indispatch’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:78:in
process' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:insynchronize’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:76:in
process' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:618:inprocess_client’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:in each' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:617:inprocess_client’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in run' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:ininitialize’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in new' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in
initialize' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:innew’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:720:in run' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb: 271:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:
270:in each' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb: 270:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:127:in
run' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/command.rb: 211:inrun’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel_rails:243
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:488:in load' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:488:inload’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:342:in new_constants_in' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:488:inload’
/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/
mongrel.rb:60
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:495:in require' /usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:342:innew_constants_in’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:495:in require' /usr/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39 /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in require' script/server:3 -e:4:inload’
-e:4

help please! :frowning:

news_controller.rb:

def show
@news = News.find(params[:id])
render_partial ‘news’
end

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

ERROR:

NoMethodError in News#show_category

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

undefined method `title’ for News:Class

Extracted source (around line #2):

1:


2:

<%=h news.title %> - Kategory: <%= news.news_category.title

i belive this has to do with how rails is trying to convert
single-plural names … you’ve chosen a kind of ambiguas one, because
news and news are both plural and singular … so what rails thinks is

@news = News.find(params[:id])
render_partial 'news'

here the variable news=a new News object (one object only) - good

render_partial 'news', :collection => @news

and here the variable news=the collection (not one object) - not good

change the @news variable in the show_category def to @newses or
something like similar … either way, that should be the problem

hth

On 19 Jul., 16:00, Shai R. [email protected]
wrote:

render_partial 'news', :collection => @news

here the variable news=a new News object (one object only) - good


Posted viahttp://www.ruby-forum.com/.

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:


2:

<%=h news.title %> - Kategory: <%= news.news_category.title
%>


3:


4: <%= bbcodeize news.content %>
5:

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?

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

the error you got was a ‘nil’ error, because no variable was passed:

you need to change this line

render_partial 'news', :collection => @newses

to this line
render :partial => ‘news’, :collection => @newses

…when you use render_partial, you are not passing the collection; it’s
a syntax thing. if you use render :partial => x , :collection => y it
should pass the collection to the partials.

hth

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

On 7/19/07, Pascal F. [email protected] wrote:

end
h1>


Extracted source (around line #2):

#{RAILS_ROOT}/app/views/news/_news.rhtml:2:in
associations/has_many_association.rb:111:in method_missing' /usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb: benchmarking.rb:26:in benchmark’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
dispatch' process_client’
271:in run' dependencies.rb:488:in load’
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in require' script/server:3 associations/has_many_association.rb:103:in method_missing’
/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:
partials.rb:58:in render_partial' filters.rb:634:in call_filter’
base.rb:430:in send' /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/rails.rb:78:in /usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel.rb:736:in run’
/usr/lib/ruby/gems/1.8/gems/mongrel-1.0.1/lib/mongrel/configurator.rb:
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
-e:4:in `load’
-e:4

help please! :frowning:


Obie F.

Pre-order my book The Rails Way today!
http://www.amazon.com/dp/0321445619

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