Rails を学習するために、hackernews に似たアプリを作成しています。すべてが開発中ですが、heroku にデプロイすると、次のエラーが発生します。
Processing by ArticlesController#index as HTML
2013-07-12T23:07:52.084981+00:00 app[web.1]: ActionView::Template::Error (undefined method `username' for nil:NilClass):
2013-07-12T23:07:52.082828+00:00 app[web.1]: Completed 500 Internal Server Error in 122ms
2013-07-12T23:07:52.084981+00:00 app[web.1]: 9: <%= pluralize(article.votes.count, 'vote') %>
2013-07-12T23:07:52.084981+00:00 app[web.1]: 12: </div>
2013-07-12T23:07:52.084981+00:00 app[web.1]: 10: by <%= link_to article.user.username, profile_path(user_id: article.user.id) %>
2013-07-12T23:07:52.084981+00:00 app[web.1]: 11: <%= time_ago_in_words(article.created_at) %> ago |
2013-07-12T23:07:52.084981+00:00 app[web.1]: 7: </div>
ここに私のユーザーモデルがあります:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username
# attr_accessible :title, :body
validates :username, presence: true, uniqueness: true
has_many :votes
has_many :articles
has_many :comments
def already_voted_on?(id)
self.votes.where(votable_id: id).count > 0
end
end
これが私のインデックス ビューです (10 行目でエラーが発生しています)。何らかの理由で、開発中は機能しているにもかかわらず、ユーザーの「ユーザー名」属性を認識していません。
<div class="articles">
<% @articles.each do |article| %>
<div class="title">
<%= link_to image_tag('votearrow.gif'), votes_path(votable_id: article.id, value: 1, votable_type: "Article"), method: :post %>
<%= link_to article.title, article.url %>
<%= image_tag("http://www.google.com/s2/favicons?domain_url=" + article.url) %>
</div>
<div class="submitted-by">
<%= pluralize(article.votes.count, 'vote') %>
by <%= link_to article.user.username, profile_path(user_id: article.user.id) %>
<%= time_ago_in_words(article.created_at) %> ago |
</div>
<div class="comments">
<%= link_to pluralize((article.comments.count), 'comment'), article_path(id: article.id) %>
</div>
<br><br>
<% end %>
</div>
助言がありますか?