【问题标题】:Error: undefined method `avatar?' for nil:NilClass错误:未定义的方法“头像?”对于零:NilClass
【发布时间】:2015-06-30 18:05:02
【问题描述】:

我在我的 rails 应用程序中收到以下错误。

错误:

NoMethodError in Statuses#index

显示 C:/Users/Arvind/project/book/app/views/statuses/index.html.erb 其中第 12 行提出:

未定义的方法“头像?”对于 nil: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___8448 'app/controllers/statuses_controller.rb:13:inindex'


我的用户.rb


 attr_accessible  :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :profile_name, :full_name, :avatar

has_attached_file :avatar, style: { large: "800x800>", medium: "300x200>", small: "260x180>", thumb: "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.url(:thumb) : 无

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


谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    问题出在 /app/helpers/application_helper.rb 的 avatar_profile_link 方法中,你必须检查 user 是否为 nil。替换为以下代码:

    def avatar_profile_link(user, image_options={}, html_options={})
      avatar_url = nil
      unless user.nil?
        avatar_url = user.avatar? ? user.avatar.url(:thumb) : nil
        link_to(image_tag(avatar_url, image_options), profile_path(user.profile_name), html_options)
      end
    end
    

    【讨论】:

    • 它给了我错误:未定义的方法user?' for #<#<Class:0x52cda90>:0x52d6e68> in same line. but when i put this code without question mark "if user" then it's give me undefined method profile_name' for nil:NilClass。
    • 我尝试在我的代码中加入“try”:def avatar_profile_link(user, image_options={}, html_options={}) avatar_url = user.try(:avatar?) ? user.avatar.url(:thumb) : nil link_to(image_tag(avatar_url, image_options), profile_path(user.try(:profile_name)), html_options) end 但没有用户出现同样的错误。
    • 对象状态不返回用户的值。也许您需要使用 current_user 方法获取用户。用户 = 当前用户。我不知道你的状态对象是如何工作的。
    • 再次出现同样的错误:未定义的方法 `avatar?'对于 nil:NilClass。
    • 这是我的 status.rb:- class Status
    【解决方案2】:

    错误信息指出:

    undefined method `avatar?' for nil:NilClass
    

    avatar_profile_link 辅助方法接受 user 参数。 user 的值是 nil 在错误发生的点。

    查看您的 git 存储库,违规部分很可能在这里:

    <% @statuses.each do |status| %>
    <% if can_display_status?(status) %>
         <div class="status media">
            <%= avatar_profile_link status.user, {}, class: 'pull-left' %>
    

    至少有一个status 拥有nil 的用户。根据您分享的内容,我们可以看到的大致如此。

    【讨论】:

    • 我真的无法自救,是不是我错了:(
    • 您可以先尝试找出status 没有关联用户的原因。状态是如何创建的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多