Rails教程 - 第11.3.2章错误(清单11.27)

时间:2011-08-28 19:02:46

标签: ruby-on-rails rspec railstutorial.org

我正在浏览railstutorial.org书,在app / controllers / pages_controller.rb中添加了一个微博实例变量后,我的应用程序的主页无法呈现,而是在浏览器上给出了以下错误:< / p>

ActionView::MissingTemplate in Pages#home

Showing /app/views/pages/home.html.erb where line #9 raised:

Missing partial shared/user_info with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/app/views"
Extracted source (around line #9):

6:         <%= render 'shared/micropost_form' %>
7:       </td>
8:       <td class="sidebar round">
9:         <%= render 'shared/user_info' %>
10:       </td>
11:     </tr>
12:   </table>
Rails.root: /home/panos/sites/sample_app

Application Trace | Framework Trace | Full Trace
app/views/pages/home.html.erb:9:in `_app_views_pages_home_html_erb__134372853_79989310__180950044'

Rspec给了我以下错误:

Failures:

  1) MicropostsController POST 'create' failure should not create a micropost
     Failure/Error: post :create, :micropost => @attr
     ActionView::Template::Error:
       Missing partial shared/user_info with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/home/panos/sites/sample_app/app/views"
     # ./app/views/pages/home.html.erb:9:in `_app_views_pages_home_html_erb__186087139_72921610__1052842684'
     # ./app/controllers/microposts_controller.rb:10:in `create'
     # ./spec/controllers/microposts_controller_spec.rb:33:in `block (5 levels) in <top (required)>'
     # ./spec/controllers/microposts_controller_spec.rb:32:in `block (4 levels) in <top (required)>'

  2) MicropostsController POST 'create' failure should render the home page
     Failure/Error: post :create, :micropost => @attr
     ActionView::Template::Error:
       Missing partial shared/user_info with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/home/panos/sites/sample_app/app/views"
     # ./app/views/pages/home.html.erb:9:in `_app_views_pages_home_html_erb__186087139_72921610__1052842684'
     # ./app/controllers/microposts_controller.rb:10:in `create'
     # ./spec/controllers/microposts_controller_spec.rb:38:in `block (4 levels) in <top (required)>'

  3) UsersController GET 'new' should be successful
     Failure/Error: get :new
     ActionView::Template::Error:
       undefined local variable or method `object' for #<#<Class:0xa89d8ac>:0xaa3a174>
     # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb__638819034_88046780__702891636'
     # ./app/views/users/new.html.erb:4:in `block in _app_views_users_new_html_erb__995846824_89246620__1052842684'
     # ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb__995846824_89246620__1052842684'
     # ./spec/controllers/users_controller_spec.rb:102:in `block (3 levels) in <top (required)>'

  4) UsersController GET 'new' should have the right title
     Failure/Error: get :new
     ActionView::Template::Error:
       undefined local variable or method `object' for #<#<Class:0xa89d8ac>:0xaa08ae8>
     # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb__638819034_88046780__702891636'
     # ./app/views/users/new.html.erb:4:in `block in _app_views_users_new_html_erb__995846824_89246620__1052842684'
     # ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb__995846824_89246620__1052842684'
     # ./spec/controllers/users_controller_spec.rb:107:in `block (3 levels) in <top (required)>'

  5) UsersController POST 'create' failure should not create a user
     Failure/Error: post :create, :user => @attr
     ActionView::Template::Error:
       undefined local variable or method `object' for #<#<Class:0xa89d8ac>:0xa99271c>
     # ./app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb__638819034_88046780__702891636'
     # ./app/views/users/new.html.erb:4:in `block in _app_views_users_new_html_erb__995846824_89246620__1052842684'
     # ./app/views/users/new.html.erb:3:in `_app_views_users_new_html_erb__995846824_89246620__1052842684'
     # ./app/controllers/users_controller.rb:30:in `create'
     # ./spec/controllers/users_controller_spec.rb:122:in `block (5 levels) in <top (required)>'
     # ./spec/controllers/users_controller_spec.rb:121:in `block (4 levels) in <top (required)>'

这是我的home.html.erb文件:

<% if signed_in? %>
  <table class="front" summary="For signed-in users">
    <tr>
      <td class="main">
        <h1 class="micropost">What's up?</h1>
        <%= render 'shared/micropost_form' %>
      </td>
      <td class="sidebar round">
        <%= render 'shared/user_info' %>
      </td>
    </tr>
  </table>
<% else %>
  <h1>Sample App</h1>

  <p>
    This is the home page for the
    <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
    sample application.
  </p>

  <%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
<% end %>

这是我的_user_info.html.erb文件:

<div class="user_info">
  <a href="<%= user_path(current_user) %>">
    <%= gravatar_for current_user, :size => 30 %>
    <span class="user_name">
      <%= current_user.name %>
    </span>
    <span class="microposts">
      <%= pluralize(current_user.microposts.count, "micropost") %>
    </span>
  </a>
</div>

我不认为我在遵循教程时犯了错误。我甚至看过类似的线程stackoverflow.com线程,但无济于事:

有没有人有任何想法?

3 个答案:

答案 0 :(得分:0)

您需要添加视图app/views/shared/_user_info.html.erb。 我猜你的路径错了。

答案 1 :(得分:0)

看起来你没有通过步骤如代码清单11.30所示:

def home
  @title = "Home"
  @micropost = Micropost.new if signed_in?
end

但我不知道为什么它反映在渲染user_info partial:(

答案 2 :(得分:0)

在app / views / shared / _user_info.html.erb

中缺少以下代码
<div class="user_info">
    <a href="<%= user_path(current_user) %>">
    <%= gravatar_for current_user, :size => 30 %>
    <span class="user_name">
        <%= current_user.name %>
    </span>
    <span class="microposts">
        <%= pluralize(current_user.microposts.count, "micropost") %>
    </span>
    </a>
</div>