ActionView :: Template ::错误:未定义的方法`email'为nil:NilClass

时间:2015-09-23 12:08:48

标签: ruby-on-rails railstutorial.org

我是Rails的新手,正在研究Michael Hartl的教程。我在第7章,一切都很好,直到我...我真的不知道。一秒钟罚款,然后我加入了gravatar_for,现在我失败了。我在这里寻找答案,有些人提到了!在downcase之后没用。这是我收到的错误消息。我知道它就在我的面前,但在花了两天时间才知道这是下一步。当我运行bundle exec rake test时,我会收到:

<!DOCTYPE html>
<html lang="en">
<head> 
    <meta charset="utf8">
</head>
<body>

    <form method="get" action="/dict/search/" enctype="mutipart/form-data">
    <div class="form-group">
         <p><input class="form-control" id="search_cyshici" name="cyshici" /></p>
    </div>
</form>

</body>
</html>

这是我的app / helpers / user_helper.rb

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

这是我的app / views / static_pages / about.html.erb

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

这是我的test / controllers / static_pages_controller_test.rb

<% 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 %>

非常感谢任何帮助!

2 个答案:

答案 0 :(得分: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教程中存在的代码(如果错误,请纠正我)。静态页面测试应该独立于任何用户,我不明白为什么你会想要第一个位置上的用户图片:)

简而言之:删除提到的代码,它应该运行正常。

答案 1 :(得分:0)

测试数据库中没有用户,因此尝试渲染时User.firstnil

<%= gravatar_for User.first, size: 50 %>
因此,{p> niluser作为gravatar_for帮助传递给email方法,并在其上调用gravatar_id = Digest::MD5::hexdigest(user.email.downcase) 方法:

user

这会导致错误。

解决方案:

  • 确保gravatar_for帮助程序中存在public class HomeFragmentActivity extends Activity { Fragment fr; Integer pagePosition; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home_fragment); pagePosition = 0; } public void categoryNavClick(View view) { fr = new FragmentOne(); FragmentManager fm = getFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragmentTransaction.setCustomAnimations(R.anim.enter_right_anim, R.anim.enter_right_anim); fragmentTransaction.replace(R.id.frHomeFragment, fr); fragmentTransaction.commit(); pagePosition = 0; } public void homeNavClick(View view) { fr = new FragmentTwo(); FragmentManager fm = getFragmentManager(); FragmentTransaction fragmentTransaction = fm.beginTransaction(); fragmentTransaction.setCustomAnimations(R.anim.enter_right_anim, R.anim.exit_right_anim); fragmentTransaction.replace(R.id.frHomeFragment, fr); fragmentTransaction.commit(); pagePosition = 1; }
  • 使用至少一个用户填充测试数据库。