Michael Hart的Ruby on Rails教程10.3.3中未定义的局部变量或方法`feed_item`

时间:2015-12-09 16:35:12

标签: ruby-on-rails ruby ruby-on-rails-4

当我运行'rails s'并访问网址“http://localhost:3000”时,发生了错误

ActionView::Template::Error (undefined local variable or method `feed_item` for #<#<Class:0xb7c6d9c>:0xb7c6310>):
1: <li id="<%= feed_item.id %>">
2: <%= link_to gravatar_for(feed_item.user), feed_item.user %>
3: <span class="user">
4: <%= link_to feed_item.user.name,feed_item.user %>
app/views/shared/_feed_item.html.erb:1:in `_app_views_shared__feed_item_html_erb___55615773_95474750`
app/views/shared/_feed.html.erb:3:in `_app_views_shared__feed_html_erb__499283038_95518450`
app/views/static_pages/home.html.erb:14:in `_app_views_static_pages_home_html_erb___238258941_96211170` `

1.app/views/shared/_feed_item.html.erb

<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name,feed_item.user %>
</span>
<span class="content"><%=feed_item.content %></span>
<span class="timestamp">
Posted <%=time_ago_in_words(feed_item.created_at) %>ago.
</span>
</li>

2.app/views/shared/_feed.html.erb

<% if @feed_items.any? %>
<ol class="microposts">
<%= render partial:'shared/feed_item',collecton:@feed_items %>
</ol>
<%= will_paginate @feed_items%>
<%end%>

3.app/controllers/static_pages_controller.rb

class StaticPagesController < ApplicationController
  def home
if signed_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page:params[:page])

end

  end
  def help
  end

  def about
  end

  def show
  end
  def contact
  end
end

4.app/views/static_pages/home.html.erb

 <% if signed_in? %>
    <div class="row" >
    <aside class="span4" >
    <section>
    <%= render 'shared/user_info' %>
    </section>
    <section>
    <%=render 'shared/micropost_form' %>
    </section>
    </aside>

    <div class="span8">
    <h3>Micropost Feed </h3>
    <%= render 'shared/feed' %>
    </div>
    </div>

    <%else%>
    <div class="center hero-unit">
    <h1>Welcome to the Sample App</h1>
    <h2>
    This is the home page for the 
    <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
    sample application.
    </h2>
    <%= link_to "Sign up now",signup_path,class: "btn btn-large btn-primary" %>
    </div>

    <%= link_to image_tag("rails.png",alt: "Rails"),'http://`rubyonrails`.org/' %>
    <%end%>

2 个答案:

答案 0 :(得分:1)

如果我正确并且你复制并粘贴了:

<ol class="microposts">
<%= render partial:'shared/feed_item', collecton: @feed_items %>
</ol>

app/views/shared/_feed.html.erb

中有拼写错误

应为collection

答案 1 :(得分:1)

您在app / views / shared / _feed.html.erb第3行中拼错了collection,尝试将其更改为: <%= render partial:'shared/feed_item', collection: @feed_items %>

您还可以找到参考实现here,它可以帮助您比较代码。