how to get paginate to work instead of getting redirected to 'public/index.html'?

痴心易碎 提交于 2019-12-13 06:26:21

问题


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>

来源:https://stackoverflow.com/questions/16307019/how-to-get-paginate-to-work-instead-of-getting-redirected-to-public-index-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!