我是Rails的新手,並且正在致力於Michael Hartl的教程。我在第七章,一切都很好,直到我......我真的不知道。一秒鐘罰款,然後我加入gravatar_for,現在我失敗了。我環顧四周尋找答案,有人提到了一個!在downcase之後沒有工作。這是我收到的錯誤消息。我知道這是正確的在我面前,但花了兩天後,這只是計算這是下一步。當我運行包的exec耙測試,我收到這樣的:ActionView :: Template :: Error:未定義的方法`電子郵件'爲零:NilClass
ERROR["test_should_get_about", StaticPagesControllerTest, 2015-09-09 15:14:50 +0000]
test_should_get_about#StaticPagesControllerTest (1441811690.06s)
ActionView::Template::Error: ActionView::Template::Error: undefined method `email' for nil:NilClass
app/helpers/users_helper.rb:5:in `gravatar_for'
app/views/static_pages/about.html.erb:13:in `_app_views_static_pages_about_html_erb__2581941383523300906_55646340'
test/controllers/static_pages_controller_test.rb:22:in `block in <class:StaticPagesControllerTest>'
app/helpers/users_helper.rb:5:in `gravatar_for'
app/views/static_pages/about.html.erb:13:in `_app_views_static_pages_about_html_erb__2581941383523300906_55646340'
test/controllers/static_pages_controller_test.rb:22:in `block in <class:StaticPagesControllerTest>'
20/20: [======] 100% Time: 00:00:00, Time: 00:00:00
這是我的應用程序/傭工/ user_helper.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
這裏是我的應用程序/視圖/ static_pages/about.html.erb
<% provide(:title, "About") %>
<h1>About</h1>
<p>
The <a href="http://www.railstutorial.org/"><em>Ruby on Rails
Tutorial</em></a> is a
<a href="http://www.railstutorial.org/book">book</a> and
<a href="http://screencasts.railstutorial.org/">screencast series</a>
to teach web development with
<a href="http://rubyonrails.org/">Ruby on Rails</a>.
This is the sample application for the tutorial.
</p>
<%= gravatar_for User.first, size: 50 %>
<%= gravatar_for User.first, size: 200 %>
<%= gravatar_for User.first, size: 80 %>
<%= gravatar_for User.first %>
,這裏是我的測試/控制器/ static_pages_controller_test.rb
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
def setup
@base_title = "Ruby on Rails Tutorial Sample App"
end
test "should get home" do
get :home
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App" end
test "should get help" do
get :help
assert_response :success
assert_select "title", "Help | #{@base_title}"
end
test "should get about" do
get :about
assert_response :success
assert_select "title", "About | #{@base_title}"
end
test "should get contact" do
get :contact
assert_response :success
assert_select "title", "Contact | #{@base_title}"
end
end
任何幫助,非常感謝!
'user.email' - > user is nil。看着我們的代碼,表明你沒有用戶,因爲這意味着'User.first'是零 –