我正在構建一個類似於hackernews的應用程序來學習Rails。一切工作在發展,但是當我部署在Heroku我收到以下錯誤:Rails未定義的方法錯誤只在生產
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>
有什麼建議?
我運行'heroku運行耙db:遷移',但它似乎仍然不工作 – Sara