【问题标题】:undefined local variable or method `feed_item` in 10.3.3 of Michael Hart's Ruby on Rails TutorialMichael Hart 的 Ruby on Rails 教程 10.3.3 中未定义的局部变量或方法 `feed_item`
【发布时间】:2015-12-09 16:35:12
【问题描述】:

当我运行 'rails s' 并访问 URL '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%>

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      如果我是对的并且你复制并粘贴了:

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

      app/views/shared/_feed.html.erb 中有错字

      应该是collection

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-28
        相关资源
        最近更新 更多