how to get paginate to work instead of getting red

2019-09-04 05:07发布

问题:

I have my homepage using 'public/index.html' which overrides everything from what I've read here.

As a result, I have all signed-in users to be redirected to http://localhost:3000/home which I have defined as get '/home' => 'static_pages#home', :as => :home in my routes

The problem I'm facing is that on the main page of signed-in users (/home), there's pagination.

When I try to click on page 2 http://localhost:3000/?page=2, it gets sent to public/index.html.

I tried the link http://localhost:3000/home/?page=2 as a test but it gives me a blank pagination. None of the items are displayed. How can I fix this?

Here's the controller

class StaticPagesController < ApplicationController
  def home
    if signed_in?
      @post = current_user.microposts.build
      @activities = PublicActivity::Activity.order("created_at desc").paginate(page: params[:page]) 
      @feed_items = current_user.feed.paginate(page: params[:page]) 
      @items = @activities + @feed_items
      @items.sort_by{|item| item.class == PublicActivity::Activity ? item.created_at : item.created_at}
      @items = @items.paginate(:page => 1, :per_page => 10)
    else
    redirect_to root_path  
    end
  end

Then in my view

  <%= render partial: 'shared/item', collection: @items %>
  <%= will_paginate @items %>

Here's the _item.html.erb partial

<li id="<%= item.id %>">
<% if item.class == PublicActivity::Activity %>
   <% if item.trackable_type == "Micropost" %>
    <%= link_to item.owner.name, item.owner if item.owner %><span class="textname"> posted</span>
  <% else %>
   <%= link_to item.owner.name, item.owner if item.owner %><span class="textname"> made a comment </span>
  <% end %>
<% else %>
<br>
  <div class ="gravatarhome"><%= link_to gravatar_for(item.user), item.user %></div>
      <%= link_to item.user.name, item.user %>
      <span class="textname">shared this
  <div class="FeedContent"><%= truncate(item.content, :length=>150, :omission=>' ...(continued)') %>
  <% end %>
     </li>