2015-09-23 67 views
1

我是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 

任何幫助,非常感謝!

+0

'user.email' - > user is nil。看着我們的代碼,表明你沒有用戶,因爲這意味着'User.first'是零 –

回答

0

在你about.html.erb你的代碼:

<%= gravatar_for User.first, size: 50 %> 
<%= gravatar_for User.first, size: 200 %> 
<%= gravatar_for User.first, size: 80 %> 
<%= gravatar_for User.first %> 

這將嘗試從數據庫中讀取並找到第一個用戶。 這個用戶似乎並不存在,我不記得那些在rails教程中的代碼(如果錯誤,糾正我)。靜態頁面測試應該是獨立於任何用戶,我不明白爲什麼你會想在第一個地方關於網站的用戶的圖片:)

總之:刪除提到的代碼,它應該運行良好。

+0

我知道這將是我愚蠢的東西。我購買了解決方案書,並且爲了插圖的目的添加了此代碼(至少這就是它說的)。非常感謝你。 – PBukuras

0

試圖使

<%= gravatar_for User.first, size: 50 %> 

nil作爲user因此傳遞到gravatar_for幫助其調用email方法上它時,有一個在你的測試數據庫中沒有用戶,從而User.firstnil

gravatar_id = Digest::MD5::hexdigest(user.email.downcase) 

這會導致錯誤。

解決方案:

  • 確保user存在於gravatar_for幫手。
  • 用至少一個用戶填充測試數據庫。
+0

感謝LapinLove404,我想如果我添加了一個用戶或兩個可以解決問題的用戶! – PBukuras

相關問題