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:
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 %>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