# Scusate: dove sbaglio?

**URL:** <https://www.ruby-forum.com/t/scusate-dove-sbaglio/102809>\
**Category:** IT\
**Created:** [July 20, 2006, 1:39pm UTC](https://www.ruby-forum.com/t/scusate-dove-sbaglio/102809 "2006-07-20T13:39:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AGAGAG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@AGAGAG](https://www.ruby-forum.com/u/AGAGAG)\
**Post date:** [July 20, 2006, 1:39pm UTC](https://www.ruby-forum.com/t/scusate-dove-sbaglio/102809/1 "2006-07-20T13:39:25Z")

</div>

Non riesco a visualizzare dopo Posted by: il nome dell’utente che ha  
creato l’articolo, al massimo solo l’id relativo…  
In cosa sbaglio? E’ un problema di relazioni tra tabelle?

- L’errore Ã¨ questo: \*

NoMethodError in Editoriale#index

Showing app/views/editoriale/\_article.rhtml where line #3 raised:

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.username

Extracted source (around line #3):

1:

  
2: 
# \<%= link\_to article.title, :action =\> ‘showNew’, :id =\> article %\>
  
3: 

Posted by \<%= article.user.username %\> \<%= distance\_of\_time\_in\_words(Time.now,article.created\_at)  
%\> ago

  
4: \<%= debug @article %\>   
5: 
\<%= article.summary %\>
  
6: 

Posted in \<%= article.category.name %\> | \<%=  
link\_to “#{article.comments.count} comments”, :action =\> ‘showNew’, :id  
=\> article %\>

- Queste sono le due tabelle (schema.rb): \*

create\_table “users”, :force =\> true do |t|  
t.column “username”, :string, :limit =\> 30, :default =\> “”, :null =\>  
false  
t.column “name”, :string, :limit =\> 30, :default =\> “”, :null =\>  
false  
t.column “surname”, :string, :limit =\> 30, :default =\> “”, :null =\>  
false  
t.column “email”, :string, :limit =\> 30, :default =\> “”, :null =\>  
false  
t.column “hashed\_password”, :string, :limit =\> 50  
t.column “age”, :integer  
t.column “sex”, :string, :limit =\> 1  
t.column “note”, :text  
t.column “role”, :string, :limit =\> 10, :default =\> “writer”, :null  
=\> false  
end

create\_table “articles”, :force =\> true do |t|  
t.column “id\_prev\_article”, :integer, :default =\> 0  
t.column “author\_id”, :integer, :default =\> 0, :null =\> false  
t.column “created\_at”, :datetime, :null =\> false  
t.column “release\_at”, :datetime, :null =\> false  
t.column “expiration\_at”, :datetime, :null =\> false  
t.column “title”, :text, :default =\> “”, :null =\> false  
t.column “summary”, :text, :default =\> “”, :null =\> false  
t.column “body”, :binary, :default =\> “”, :null =\> false  
t.column “category\_id”, :integer  
t.column “editorial\_status”, :string, :limit =\> 10  
t.column “editor\_id”, :integer  
t.column “editorial\_status\_date”, :datetime  
end

- E questi i due modelli: \*  
class Article \< ActiveRecord::Base  
belongs\_to :user, :class\_name =\> ‘User’, :foreign\_key =\> “author\_id”,  
:foreign\_key =\> “editor\_id”

belongs\_to :category  
has\_many :comments

class User \< ActiveRecord::Base  
has\_many :articles  
attr\_protected :role  
attr\_accessor :password, :password\_confirmation;  
#validates\_length\_of :password, :minimum =\> 5, :message =\> “should be  
at least 5 characters long”  
validates\_presence\_of :name, :username  
validates\_uniqueness\_of :username, :message =\> “already exists”  
validates\_numericality\_of :age, :message =\> “Invalid format of age”  
before\_destroy :dont\_destroy\_admin  
validates\_format\_of :email,  
:with =\>  
/^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,3})$/,  
:message =\> ‘must be a valid email address’

- Con questa view non riesco in alcun modo a visualizzare il nome  
dell’autore dell’articolo --\> /\_article.rhtml: \*

# \<%= link\_to article.title, :action =\> 'showNew', :id =\> article %\>

Posted by \<%= article.user.username %\> \<%= distance\_of\_time\_in\_words(Time.now,article.created\_at) %\> ago

\<%= debug @article %\> 
\<%= article.summary %\>

Posted in \<%= article.category.name %\> | \<%= link\_to "#{article.comments.count} comments", :action =\> 'showNew', :id =\> article %\>

- Ecco il controller (editoriale\_controller.rb): \*

def list\_published  
@categories = Category.find(:all, :order =\> ‘id asc’, :limit =\> 5)  
@articles\_pages, @articles = paginate(:articles, :conditions =\>  
“editorial\_status = ‘approved’”, :order =\> ‘id desc’)  
end

Vi ringrazio in anticipo.

Alessandro

---

<div class="post-metadata">

**Author:** ![AGAGAG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@AGAGAG](https://www.ruby-forum.com/u/AGAGAG)\
**Post date:** [July 20, 2006, 3:54pm UTC](https://www.ruby-forum.com/t/scusate-dove-sbaglio/102809/2 "2006-07-20T15:54:13Z")

</div>

AG ha scritto:

> You have a nil object when you didn’t expect it!  
> 4: \<%= debug @article %\>   
> 5:
> 
> \<%= article.summary %\>
>   
> 6: 
> 
> Posted in \<%= article.category.name %\> | \<%=  
> link\_to “#{article.comments.count} comments”, :action =\> ‘showNew’, :id  
> =\> article %\>

L’errore parla di un metodo della classe Editoriale non trovato (index)  
pasta magari il backtrace dell’eccezione

* * *

rpl

---

<div class="post-metadata">

**Author:** ![AGAGAG](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@AGAGAG](https://www.ruby-forum.com/u/AGAGAG)\
**Post date:** [July 21, 2006, 9:59am UTC](https://www.ruby-forum.com/t/scusate-dove-sbaglio/102809/3 "2006-07-21T09:59:25Z")

</div>

Ecco:

NoMethodError in Editoriale#index

Showing app/views/editoriale/\_article.rhtml where line #3 raised:

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.username

Extracted source (around line #3):

1:

  
2: 
# \<%= link\_to article.title, :action =\> ‘showNew’, :id =\> article %\>
  
3: 

Posted by \<%= article.user.username %\> \<%= distance\_of\_time\_in\_words(Time.now,article.created\_at)  
%\> ago

  
4: \<%= debug @article %\>   
5: 
\<%= article.summary %\>
  
6: 

Posted in \<%= article.category.name %\> | \<%=  
link\_to “#{article.comments.count} comments”, :action =\> ‘showNew’, :id  
=\> article %\>

Trace of template inclusion: /app/views/editoriale/articles.rhtml

RAILS\_ROOT: /var/www/editoriale/public/…/config/…  
Application Trace | Framework Trace | Full Trace

#{RAILS\_ROOT}/app/views/editoriale/\_article.rhtml:3:in  
`_run_rhtml_editoriale__article' #{RAILS_ROOT}/app/views/editoriale/articles.rhtml:4:in`\_run\_rhtml\_editoriale\_articles’  
#{RAILS\_ROOT}/app/views/editoriale/articles.rhtml:2:in  
`_run_rhtml_editoriale_articles' #{RAILS_ROOT}/app/controllers/editoriale_controller.rb:5:in`index’

/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/base.rb:314:in  
`compile_and_render_template' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:290:in`render\_template’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/base.rb:249:in  
`render_file' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:264:in`render’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/partials.rb:59:in  
`render_partial' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:29:in`benchmark’  
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure' /usr/lib/ruby/1.8/benchmark.rb:307:in`realtime’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/benchmarking.rb:29:in  
`benchmark' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/partials.rb:58:in`render\_partial’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/base.rb:314:in  
`compile_and_render_template' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:290:in`render\_template’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/base.rb:249:in  
`render_file' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:699:in`render\_file’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/base.rb:621:in  
`render_with_no_layout' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:742:in`render\_without\_layout’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/base.rb:691:in  
`render_action' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:643:in`render\_with\_no\_layout’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/layout.rb:240:in  
`render_without_benchmark' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in`render’  
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:53:in`render’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/base.rb:910:in  
`perform_action_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in`perform\_action\_without\_benchmark’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:in`measure’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in`perform\_action’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/base.rb:381:in  
`process_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in`process\_without\_session\_management\_support’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/session\_management.rb:117:in  
`process' /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in`dispatch’  
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi\_handler.rb:150:in  
`process_request' /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:54:in`process!’  
/usr/lib/ruby/1.8/fcgi.rb:600:in `each_cgi' /usr/lib/ruby/1.8/fcgi.rb:597:in`each\_cgi’  
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi\_handler.rb:53:in  
`process!' /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:23:in`process!’  
/var/www/editoriale/public/dispatch.fcgi:24

#{RAILS\_ROOT}/app/views/editoriale/\_article.rhtml:3:in  
`_run_rhtml_editoriale__article' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:314:in`compile\_and\_render\_template’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/base.rb:290:in  
`render_template' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:249:in`render\_file’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/base.rb:264:in  
`render' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/partials.rb:59:in`render\_partial’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/benchmarking.rb:29:in  
`benchmark' /usr/lib/ruby/1.8/benchmark.rb:293:in`measure’  
/usr/lib/ruby/1.8/benchmark.rb:307:in `realtime' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:29:in`benchmark’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/partials.rb:58:in  
`render_partial' #{RAILS_ROOT}/app/views/editoriale/articles.rhtml:4:in`\_run\_rhtml\_editoriale\_articles’  
#{RAILS\_ROOT}/app/views/editoriale/articles.rhtml:2:in  
`_run_rhtml_editoriale_articles' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:314:in`compile\_and\_render\_template’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_view/base.rb:290:in  
`render_template' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_view/base.rb:249:in`render\_file’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/base.rb:699:in  
`render_file' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:621:in`render\_with\_no\_layout’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/base.rb:742:in  
`render_without_layout' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:691:in`render\_action’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/base.rb:643:in  
`render_with_no_layout' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/layout.rb:240:in`render\_without\_benchmark’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/benchmarking.rb:53:in  
`render' /usr/lib/ruby/1.8/benchmark.rb:293:in`measure’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/benchmarking.rb:53:in  
`render' #{RAILS_ROOT}/app/controllers/editoriale_controller.rb:5:in`index’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/base.rb:910:in  
`perform_action_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in`perform\_action\_without\_benchmark’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' /usr/lib/ruby/1.8/benchmark.rb:293:in`measure’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in`perform\_action’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/base.rb:381:in  
`process_without_filters' /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in`process\_without\_session\_management\_support’  
/usr/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/session\_management.rb:117:in  
`process' /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in`dispatch’  
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi\_handler.rb:150:in  
`process_request' /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:54:in`process!’  
/usr/lib/ruby/1.8/fcgi.rb:600:in `each_cgi' /usr/lib/ruby/1.8/fcgi.rb:597:in`each\_cgi’  
/usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi\_handler.rb:53:in  
`process!' /usr/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/fcgi_handler.rb:23:in`process!’  
/var/www/editoriale/public/dispatch.fcgi:24

Request

Parameters: None

Show session dump

* * *

flash: !map:ActionController::Flash::FlashHash {}

Response  
Headers: {“cookie”=\>[], “Cache-Control”=\>“no-cache”}

Ripley ha scritto:
