Nothing obviously wrong so I'll guess something obscure...
have you
added an inflection rule telling Rails that 'news' is
uncountable (like
fish, it uses the same for singular and plural)? Check the
end of
/config/environment.rb. You'd uncomment in the inflection
rules at the
end with the result looking like:
Inflector.inflections do |inflect|
inflect.uncountable %w(fish sheep news)
end
Just wondering if Rails is getting confused over the
has_many/belongs_to relationship between :news and :user.
Also, you might want to check your News.find_recent method.
As written
it appears that it will always return the first ten news
items created
and return them in reverse order. Instead you probably want
the db to
return the last ten stories created:
class News < ActiveRecord::Base
...
def self.find_recent
News.find(:all, :order => 'created_at DESC', :limit
=> 10)
end
end
Good luck,
AndyV
askegg wrote:
> Hi all,
>
> I have a NewsModel that looks like this:
>
> class News < ActiveRecord::Base
>
> belongs_to :user
>
> validates_associated :user
> validates_length_of :title, :description, :minimum
=> 5, :allow_nil
> => false
>
> acts_as_commentable
> acts_as_taggable
>
> def self.find_recent
> News.find(:all, :order => 'created_at', :limit
=> 10).reverse
> end
>
> end
>
> and a controller like this:
> class NewsController < ApplicationController
>
> def show
> begin
> news = News.find(params[:id])
> rescue ActiveRecord::RecordNotFound
> flash[:notice] = "News item not found!"
> redirect_to :action => 'index' and return
> else
> comments = news.comments
> end
> end
> end
>
> and finally the view:
> --Show
> <div id="news">
> <%= render :partial => '/news/item', :locals
=> { :item => news } %>
>
> <%= render :partial => '/comments/comment',
:collection => comments
> %>
>
> <%= render :partial => '/comments/form' %>
> </div>
>
> --/comments/comment
> <div class="comment">
> <h2>Comments</h2>
> <div class="comment_title">
> <%= comment.title -%>
> </div>
> <div class="comment.text">
> <%= h comment.comment -%>
> </div>
> <div class="comment_author">
> <%= comment.user.fullname -%> <%=
> time_ago_in_words(comment.created_at) -%> ago
> </div>
> <hr>
> </div>
>
> As you can see, the comments partial calls
comment.user.fullname, which
> is defined in the UserModel:
> class User < ActiveRecord::Base
>
> has_many :documents
> has_many :events
> has_many :news
> has_many :comments
>
> def fullname
> "# #"
> end
>
> end
>
>
>
> When the server is first started the page renders
correctly, HOWEVER if
> I hit refresh it fails with:
> undefined method `fullname' for #<User:0x357e380>
>
>
>
> Extracted source (around line #10):
>
> 7: <%= h comment.comment -%>
> 8: </div>
> 9: <div class="comment_author">
> 10: <%= comment.user.fullname -%> <%=
> time_ago_in_words(comment.created_at) -%> ago
> 11: </div>
> 12: <hr>
> 13: </div>
>
> Restarting the server fixes the issue for 1 view, then
all subsequent
> views fail. Here is the console log:
>
> Andrews-MacBook:~/Sites/tassia andrewskegg$
./script/server
> => Booting Mongrel (use 'script/server webrick' to
force WEBrick)
> => Rails application starting on http://0.0.0.0:3000
> => Call with -d to detach
> => Ctrl-C to shutdown server
> ** Starting Mongrel listening at 0.0.0.0:3000
> ** Starting Rails with development environment...
> ** Rails loaded.
> ** Loading any Rails specific GemPlugins
> ** Signals ready. TERM => stop. USR2 =>
restart. INT => stop (no
> restart).
> ** Rails signals registered. HUP => reload (without
restart). It
> might not work well.
> ** Mongrel available at 0.0.0.0:3000
> ** Use CTRL-C to stop.
>
>
> Processing NewsController#show (for 127.0.0.1 at
2006-12-20 10:49:28)
> [GET]
> Session ID: ea9a336b5b4d1bee4d647d25de2a27e7
> Parameters: {"action"=>"show",
"id"=>"13",
"controller"=>"news"}
> News Columns (0.000266) SHOW FIELDS FROM news
> News Load (0.000302) SELECT * FROM news WHERE
(news.id = 13)
> Rendering within layouts/application
> Rendering news/show
> Tagging Columns (0.000286) SHOW FIELDS FROM
taggings
> Tag Load (0.000444) SELECT tags.* FROM tags INNER
JOIN taggings ON
> tags.id = taggings.tag_id WHERE
((taggings.taggable_type = 'News') AND
> (taggings.taggable_id = 13))
> Tag Columns (0.000187) SHOW FIELDS FROM tags
> Rendered /news/_item (0.00561)
> Comment Load (0.000301) SELECT * FROM comments
WHERE
> (comments.commentable_id = 13 AND
comments.commentable_type = 'News')
> Comment Columns (0.000248) SHOW FIELDS FROM
comments
> User Columns (0.000345) SHOW FIELDS FROM users
> User Load (0.000359) SELECT * FROM users WHERE
(users.id = 1)
> Rendered /comments/_comment (0.01381)
> Rendered /comments/_form (0.00201)
> Completed in 0.04776 (20 reqs/sec) | Rendering: 0.02942
(61%) | DB:
> 0.00274 (5%) | 200 OK [http://localhost/news/s
how/13]
>
>
> Processing NewsController#show (for 127.0.0.1 at
2006-12-20 10:49:32)
> [GET]
> Session ID: ea9a336b5b4d1bee4d647d25de2a27e7
> Parameters: {"action"=>"show",
"id"=>"13",
"controller"=>"news"}
> News Columns (0.000273) SHOW FIELDS FROM news
> News Load (0.000314) SELECT * FROM news WHERE
(news.id = 13)
> Rendering within layouts/application
> Rendering news/show
> Tag Load (0.000489) SELECT tags.* FROM tags INNER
JOIN taggings ON
> tags.id = taggings.tag_id WHERE
((taggings.taggable_type = 'News') AND
> (taggings.taggable_id = 13))
> Rendered /news/_item (0.00340)
> Comment Load (0.000447) SELECT * FROM comments
WHERE
> (comments.commentable_id = 13 AND
comments.commentable_type = 'News')
> User Columns (0.000271) SHOW FIELDS FROM users
> User Load (0.000356) SELECT * FROM users WHERE
(users.id = 1)
>
>
> ActionView::TemplateError (undefined method `fullname'
for
> #<User:0x357e380>) on line #10 of
app/views/comments/_comment.rhtml:
> 7: <%= h comment.comment -%>
> 8: </div>
> 9: <div class="comment_author">
> 10: <%= comment.user.fullname -%>
<%=
> time_ago_in_words(comment.created_at) -%> ago
> 11: </div>
> 12: <hr>
> 13: </div>
>
>
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/l
ib/active_record/base.rb:1848:in
> `method_missing'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/l
ib/active_record/associations/association_proxy.rb:123:in
> `send'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/l
ib/active_record/associations/association_proxy.rb:123:in
> `method_missing'
>
#/app/views//comments/_comment.rhtml:10:in
> `_run_rhtml_47app47views47comments47_comment46rhtml'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/base.rb:326:in
> `send'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/base.rb:326:in
> `compile_and_render_template'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/base.rb:301:in
> `render_template'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/base.rb:260:in
> `render_file'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/base.rb:275:in
> `render'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/partials.rb:59:in
> `render_partial'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/benchmarking.rb:26:in
> `benchmark'
> /usr/local/lib/ruby/1.8/benchmark.rb:293:in
`measure'
> /usr/local/lib/ruby/1.8/benchmark.rb:307:in
`realtime'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/benchmarking.rb:26:in
> `benchmark'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/partials.rb:58:in
> `render_partial'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/partials.rb:73:in
> `render_partial_collection'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/l
ib/active_record/base.rb:1361:in
> `each_with_index'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/l
ib/active_record/associations/association_proxy.rb:123:in
> `each'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/l
ib/active_record/associations/association_proxy.rb:123:in
> `each_with_index'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/l
ib/active_record/associations/association_proxy.rb:123:in
> `send'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/l
ib/active_record/associations/association_proxy.rb:123:in
> `method_missing'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4.5618/l
ib/active_record/associations/has_many_association.rb:98:in
> `method_missing'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/partials.rb:71:in
> `render_partial_collection'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/base.rb:285:in
> `render'
> #/app/views/news/show.rhtml:4:in
> `_run_rhtml_47app47views47news47show46rhtml'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/base.rb:326:in
> `send'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/base.rb:326:in
> `compile_and_render_template'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/base.rb:301:in
> `render_template'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_view/base.rb:260:in
> `render_file'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/base.rb:786:in
> `render_file'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/base.rb:694:in
> `render_with_no_layout'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/layout.rb:247:in
> `render_without_benchmark'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/benchmarking.rb:50:in
> `render'
> /usr/local/lib/ruby/1.8/benchmark.rb:293:in
`measure'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/benchmarking.rb:50:in
> `render'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/base.rb:1069:in
> `perform_action_without_filters'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/filters.rb:635:in
> `call_filter'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/filters.rb:622:in
> `perform_action_without_benchmark'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/benchmarking.rb:66:in
> `perform_action_without_rescue'
> /usr/local/lib/ruby/1.8/benchmark.rb:293:in
`measure'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/benchmarking.rb:66:in
> `perform_action_without_rescue'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/rescue.rb:81:in
> `perform_action'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/base.rb:427:in
> `send'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/base.rb:427:in
> `process_without_filters'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/filters.rb:627:in
> `process_without_session_management_support'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/session_management.rb:114:in
> `process'
>
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/base.rb:330:in
> `process'
>
>
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6.5618/lib/dispa
tcher.rb:41:in
> `dispatch'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el/rails.rb:84:in
> `process'
> /usr/local/lib/ruby/1.8/sync.rb:229:in
`synchronize'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el/rails.rb:83:in
> `process'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el.rb:580:in
> `process_client'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el.rb:579:in
> `each'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el.rb:579:in
> `process_client'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el.rb:686:in
> `run'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el.rb:686:in
> `initialize'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el.rb:686:in
> `new'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el.rb:686:in
> `run'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el.rb:673:in
> `initialize'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el.rb:673:in
> `new'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el.rb:673:in
> `run'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el/configurator.rb:267:in
> `run'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el/configurator.rb:266:in
> `each'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el/configurator.rb:266:in
> `run'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/bin/mongr
el_rails:127:in
> `run'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/lib/mongr
el/command.rb:211:in
> `run'
>
>
/usr/local/lib/ruby/gems/1.8/gems/mongrel-0.3.13.4/bin/mongr
el_rails:231
>
>
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/l
ib/active_support/dependencies.rb:482:in
> `load'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/l
ib/active_support/dependencies.rb:482:in
> `load'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/l
ib/active_support/dependencies.rb:337:in
> `new_constants_in'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/l
ib/active_support/dependencies.rb:482:in
> `load'
>
>
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6.5618/lib/comma
nds/servers/mongrel.rb:52
>
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb
:27:in
> `gem_original_require'
>
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb
:27:in
> `require'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/l
ib/active_support/dependencies.rb:489:in
> `require'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/l
ib/active_support/dependencies.rb:337:in
> `new_constants_in'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/l
ib/active_support/dependencies.rb:489:in
> `require'
>
>
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6.5618/lib/comma
nds/server.rb:39
>
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb
:27:in
> `gem_original_require'
>
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb
:27:in
> `require'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/l
ib/active_support/dependencies.rb:489:in
> `require'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/l
ib/active_support/dependencies.rb:337:in
> `new_constants_in'
>
>
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1.5618/l
ib/active_support/dependencies.rb:489:in
> `require'
> script/server:3
>
>
> Rendering
>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5.5618/lib
/action_controller/templates/rescues/layout.rhtml
> (500 Internal Error)
>
>
>
> Any ideas?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrails googlegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups-beta.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---
|