Scusate: dove sbaglio?

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

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

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:inindex’

/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:inrender_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:inrender’
/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: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.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:inrender_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:inrender_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:inrender_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:inrender_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:inrender_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:inrender’
/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:inrender’
/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:inperform_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:inmeasure’
/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:inperform_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:inprocess_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:indispatch’
/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:inprocess!’
/usr/lib/ruby/1.8/fcgi.rb:600:in each_cgi' /usr/lib/ruby/1.8/fcgi.rb:597:ineach_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:inprocess!’
/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:incompile_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:inrender_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:inrender_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:inmeasure’
/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:inbenchmark’
/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:incompile_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:inrender_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:inrender_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:inrender_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:inrender_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:inmeasure’
/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:inindex’
/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:inperform_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:inmeasure’
/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:inperform_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:inprocess_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:indispatch’
/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:inprocess!’
/usr/lib/ruby/1.8/fcgi.rb:600:in each_cgi' /usr/lib/ruby/1.8/fcgi.rb:597:ineach_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:inprocess!’
/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:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs