NoMethodError в PagesController # home - PullRequest
       8

NoMethodError в PagesController # home

3 голосов
/ 03 января 2012

Я работаю в руководстве по Rails Tutorial, глава 12, и получаю следующую ошибку на домашней / главной странице, когда пользователь вышел из системы (вход в систему в порядке):

Я новичок в Railsпоэтому, пожалуйста, будьте явны в своем ответе!Большое спасибо ..

NoMethodError в PagesController # home

undefined method `feed' for nil:NilClass

Rails.root: / Users / fkhalid2008 / Documents / First-app

Трассировка приложений |Framework Trace |Full Trace

app/controllers/pages_controller.rb:6:in `home'

Контроллер страниц

class PagesController < ApplicationController

def home
  @title = "Home"
  @post = Post.new if signed_in?
  @feed_items = current_user.feed.paginate(:page => params[:page])
end

def contact
  @title = "Contact"
end

def about
  @title = "About Us"
end

end

Просмотр домашней страницы (/app/views/pages/home.html.erb)

<% if signed_in? %>
<table class="front" summary="For signed-in users">
<tr>
  <td class="main">
    <h1 class="post">What's up?</h1>
    <%= render 'shared/post_form' %>
    <%= render 'shared/feed' %>
  </td>
  <td class="sidebar round">
    <%= render 'shared/user_info' %>
  </td>
</tr>
</table>
<% else %>
<h1>Palazzo Valencia</h1>

<p>
This is the home page for the
<a href="http://railstutorial.org/">Palazzo Valencia</a>
sample application.
</p>

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

Частичная подача

<% unless @feed_items.empty? %>
<table class="posts" summary="User posts">
<%= render :partial => 'shared/feed_item', :collection => @feed_items %>
</table>
<%= will_paginate @feed_items %>
<% end %>

Частичная подача

<tr>
<td class="gravatar">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
</td>
<td class="post">
<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>
</td>
<% if current_user?(feed_item.user) %>
<td>
<%= link_to "delete", feed_item, :method => :delete,
                                 :confirm => "You sure?",
                                 :title => feed_item.content %>
</td>
<% end %>
</tr>

Ответы [ 2 ]

5 голосов
/ 03 января 2012

Пользователь не вошел в систему. Поэтому метод current_user возвращает ноль, а ruby ​​не может найти метод feed.

Вы можете изменить код на это:

@title = "Home"
if signed_in?
    @post = Post.new
    @feed_items = current_user.feed.paginate(:page => params[:page])
end

Теперь новая запись и элементы фида будут извлечены, только когда пользователь вошел в систему.

0 голосов
/ 27 октября 2015

app / controllers / static_pages_controller.rb

  def home
    if logged_in?
      @micropost  = current_user.microposts.build
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end
...