我在rails应用程序中收到以下错误 .
错误:
NoMethodError in Statuses#index
显示C:/Users/Arvind/project/book/app/views/statuses/index.html.erb第12行引发:
未定义的方法`avatar?'为零:NilClass
Rails.root:C:/ Users / Arvind / project / book应用程序跟踪|框架跟踪|完整跟踪
app / helpers / applicationhelper.rb:28:in avatar_profile_link'app / views / statuses / index.html.erb:12:inblock in _app_views_statuses_index_html_erb__844883668_70166988'app / views / statuses / index.html.erb:9:in _app_views_statuses_index_html_erb___844883668_70166988'app /控制器/ statuses_controller.rb:13:inindex”
我的user.rb
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :profile_name, :full_name, :avatar
has_attached_file:头像,风格:{large:“800x800>”,中:“300x200>”,小:“260x180>”,拇指:“80x80#”}
def self.get_gravatars
all.each do |user|
if !user.avatar?
user.avatar = URI.parse(user.gravatar_url)
user.save
print "."
end
end
end
我的application_helper.rb
def avatar_profile_link(user, image_options={}, html_options={})
avatar_url = user.avatar? ? user.avatar.url(:thumb):nil
link_to(image_tag(avatar_url, image_options), profile_path(user.profile_name), html_options)
结束
我的lib / task / gravatars,rake
desc "Import avatars from user's gravatar url"
task :import_avatars => :environment do
puts "Importing avatars from gravatar"
User.get_gravatars
puts "Avatars updated."
end
我将我的头像更新为gravatar
$rake import avatar
将头像导入gravatar
.....阿凡达更新 .
我的git存储库位于:https://github.com/sarahgupta022/book.git
谢谢!
2 回答
问题出在avatar_profile_link方法的/app/helpers/application_helper.rb中,你必须检查用户是否为零 . 替换为此代码:
错误消息指出:
avatar_profile_link
辅助方法接受user
参数 .user
的值在发生错误时为nil
.看看你的git repo,违规部分最有可能在这里:
至少有一个
status
的用户为nil
. 根据您分享的内容,我们可以看到这一点 .