Reputation: 2769
I'm building an app similar to hackernews to learn Rails. Everything is working in development, but when I deploy on heroku I get the following error:
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>
Here's my user model:
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
And here's my index view (line 10 is throwing the error). For some reason it's not recognizing a user's "username" attribute even though it works in development:
<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>
Any suggestions?
Upvotes: 0
Views: 978
Reputation: 2769
The problem was heroku wasn't updating my latest migration (AddUserIdToArticles). I fixed the problem by deleting the column in the database, adding it again, and running heroku restart.
Upvotes: 1
Reputation: 1071
Username is not a devise's field, so maybe you forgot to run your migrations in production environment?
Upvotes: 2