gravatar_for没有方法错误Hartl rails第7章

时间:2017-11-18 15:24:30

标签: ruby erb nomethoderror gravatar ruby-on-rails-5

在尝试定义gravatar_for并调用它之前,一切正常。 user.email.downcase是零级。以下是我正在使用的内容。如果您需要任何其他信息,请与我们联系。

我在url http://localhost:3000/users/1运行localhost时看到了 NoMethodError in Users#show

`Showing /Users/nathandewaele/Desktop/workspce/static_pages/app/views/users/show.html.erb where line #6 raised:

undefined method `downcase' for nil:NilClass`

在第5行提取源 gravatar_id = Digest::MD5::hexdigest(user.email.downcase)

`Rails.root: /Users/nathandewaele/Desktop/workspce/static_pages

Application Trace | Framework Trace | Full Trace
app/helpers/users_helper.rb:5:in `gravatar_for'
app/views/users/show.html.erb:6:in `_app_views_users_show_html_erb___4114710587947303689_70365956197180'
Request

Parameters:

{"id"=>"1"}
Toggle session dump
Toggle env dump
Response

Headers:

None`

的应用程序/助手/ users_helper.rb

```rb

module UsersHelper

  # Returns the Gravatar for the given user.
  def gravatar_for(user, options = { size: 80 })
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    size = options[:size]
    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
    image_tag(gravatar_url, alt: user.name, class: "gravatar")
  end
end
```

的应用程序/控制器/ users_controller.rb

```ruby

class UsersController < ApplicationController

    def show
    @user = User.find(params[:id])
  end

  def new
        @user = User.new
  end
end
```

的应用程序/分贝/ schema.rb

```ruby

ActiveRecord::Schema.define(version: 20171108202327) do

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "email"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "password_digest"
    t.index ["email"], name: "index_users_on_email", unique: true
  end

end
```

的应用程序/控制器/配置/ routes.rb中

```ruby

Rails.application.routes.draw do

  # get '/new', to: 'users#new'
    get '/show', to: 'users#show'
    get  '/help', to: 'static_pages#help'
    get '/about', to: 'static_pages#about'
    get '/contact', to: 'static_pages#contact'
    get  '/signup',  to: 'users#new'

    resources :users
end
```

的应用程序/视图/用户/ show.html.erb

```erb

<% provide(:title, @user.name) %>
<div class="row">
  <aside class="col-md-4">
    <section class="user_info">
      <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
      </h1>
    </section>
  </aside>
</div>
```

screenshot of error

2 个答案:

答案 0 :(得分:1)

有同样的问题,我用代码更新/ app / helpers / users_helper时解决了:

  # Returns the Gravatar for the given user.
  def gravatar_for(user)
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
    image_tag(gravatar_url, alt: user.name, class: "gravatar")
  end

(如清单7.9所示)

答案 1 :(得分:0)

我遇到了同样的问题,很难追查!如上所述,电子邮件未发送到数据库。在我更改了模型/ users.rb文件中的代码后,我的问题似乎解决了,我自己也困惑了: before_save { self.email = email.downcase }

在我使用before_save { self.email = email.downcase! }之前。验证位于Rails教程的第6章。不知何故,我感到困惑email.downcase!与self.email = email.downcase

这仍然留下了为什么在没有电子邮件的情况下创建帐户的原因,而不是一个错误阻止我继续...