2017-05-09 47 views
0

我是Rails的新手,並且一直關注在線教程,其中涉及創建一個簡單的博客站點。我正在爲控制器和模型添加測試項目。當我嘗試運行posts_controller_test.rb,我得到以下錯誤:錯誤:未定義的方法名稱爲零:NilClass

Error: 
PostsControllerTest#test_should_get_index: 
ActionView::Template::Error: undefined method `name' for nil:NilClass 
    app/views/posts/index.html.erb:7:in `block in _app_views_posts_index_html_erb__805998677_54338856' 
    app/views/posts/index.html.erb:1:in `_app_views_posts_index_html_erb__805998677_54338856' 
    test/controllers/posts_controller_test.rb:5:in `block in <class:PostsControllerTest>' 

bin/rails test test/controllers/posts_controller_test.rb:4 

這裏是我的posts_controller_test.rb:

class PostsControllerTest < ActionDispatch::IntegrationTest 
    test "should get index" do 
    get posts_url 
    assert_response :success 
    assert_not_nil assigns(:posts) 
    end 
end 

的教程是在早期版本的軌道,其用於生產get:index,我在rails 5中不贊成使用它,建議使用URL而不是動作名稱。

有趣的是,如果我執行控制器home_controller_test.rb,我還沒有修改,我得到如下不同的錯誤:

Error: 
HomeControllerTest#test_should_get_index: 
NameError: undefined local variable or method `home_index_url' for #<HomeControllerTest:0x67df790> 
    test/controllers/home_controller_test.rb:5:in `block in <class:HomeControllerTest>' 

bin/rails test test/controllers/home_controller_test.rb:4 

這裏是home_controller_test.rb:

require 'test_helper' 

class HomeControllerTest < ActionDispatch::IntegrationTest 
    test "should get index" do 
    get home_index_url 
    assert_response :success 
    end 
end 

任何有關這些問題可能的幫助將不勝感激。

添加我的帖子索引視圖:

<% @posts.each do |post| %> 
    <div class="post"> 
     <h2 class="title"><%= link_to post[:title],post %></h2> 
     <div class="entry"><%= post[:body] %></div> 
     </br> 
     <div class="byline"> 
      <span class="meta"><%= post[:created_at].strftime("%b %d, %Y") %> Posted By: <%= post.admin_user.name %></span><span class="links"><%= link_to "Read More",post %> 
     </div> 
    </div> 
<% end %> 
+0

分享你的'索引'視圖 –

+0

這意味着,這個文件:app/views/posts/index.html.erb :) – Gerry

+0

我在上面分享了我的索引視圖。 – henrypj

回答

0

更新(2017年5月10日)

你的帖子模型admin_user_id主要指專屬於管理用戶。

您的開發rails應用程序不會觸發錯誤,因爲它始終爲每個帖子鏈接AdminUser。但在測試環境中,您需要在test/fixtures文件夾下設置數據庫數據。

在你的情況,你需要創建測試/夾具/ admin_users.yml一個管理用戶(例如,adminUserOne),並與ADMINUSER鏈接您的文章像

postOne: 
    admin_user: adminUserOne 

那麼只有您的帖子已鏈接admin_user 。

良好做法,可能是你剛剛檢查是否存在管理用戶的,並給予不同的輸出,如果admin_user不存在

<%= post.admin_user.present? post.admin_user.name : '-' %> 

對於第一個錯誤

undefined method `name' for nil:NilClass

app/views/posts/index.html.erb:7

錯誤消息指出位於提到的視圖文件的第7行的錯誤。請參閱您的視圖文件的內容後,它是由

post.admin_user.name 

在Post模型引起的,它沒有與admin_user任何關係。 通常我們可以檢查db/schema中的關係。與沙盒模式

rails c --sandbox 

RB或運行軌道控制檯,然後嘗試做一些像

Post.first.admin_user 

觀察什麼結果將在控制檯中給出。


對於第二個錯誤,

的home_index_url是不是在你的Rails應用程序有效的URL。

在軌5,你可以使用命令

rails routes -c home 

對發現任何相關的路由。在我的應用程序中,我有這個結果

Prefix

dashboard

root

所以我有dashboard_url和root_url。所以在你的情況下,應該有前綴home_index的路線,那麼只有你可以使用home_index_url

+0

謝謝。我明白你現在在說什麼...... Posts模型有admin_user_id而不是admin_user。所以當在控制檯中執行'Post.first.admin_user'時,它確實是零。正如我所提到的,我是RoR的新手,並且正在接受在線教程。我不明白的是,視圖中的post.admin_user.name似乎在瀏覽器中正常工作......名稱實際上是顯示的。 – henrypj

+0

@henrypj我更新了我的帖子,有一個檢查,時刻謹防鏈接對象的可能性爲空,如果在保存之前沒有任何驗證 –

+0

再次感謝,這是有道理的。 – henrypj

相關問題