0

How do I get this page to paginate using kaminari? I need help with this badly. I don't know what to do. Just get me some way so that all the posts will only be 15 per page. I want the show page to be paginated not the index page.

This is the website I built. http://duelingpets.net/maintopics/9/subtopics/4

This is my show page for my subtopics page that is nested under maintopics

<p id="notice"><%= notice %></p>
This pages shows the current topic name of a subtopic
<h1 class="forumheader"><%= @subtopic.topicname %></h1>
<br><!---Gives space to start the narratives--->
<div class="narrativecontainer">
   <table>
      <tr>
         <td><%= @subtopic.topicname %></td>
      </tr>
      <tr>
         <td>by: <%= @subtopic.user.vname %></td>
      </tr>
      <tr>
         <td><pre class="narrative_pre"><%= @subtopic.description %></pre></td>
      </tr>
   </table>
   <br>
   <% @subtopic.narratives.each do |narrative| %>
      <div class="outer">
         <table>
            <tr>
               <td>re:<%= narrative.subtopic.topicname %></td>
               <% if current_user && (current_user.id == narrative.user_id || current_user.admin?)%>
                  <td><%= button_to 'Edit', edit_subtopic_narrative_path(@subtopic, narrative), method: :get %></td>
                  <td><%= button_to 'Destroy', [@subtopic, narrative], method: :delete, data: { confirm: 'Are you sure?' } %></td>
               <% end %>
            </tr>
            <tr>
               <td>by: <%= narrative.user.vname %><%#= subtopic.description %></td>
            </tr>
         </table>
      </div>
      <div class="outer">
         <pre class="narrative_pre"><%= narrative.story %></pre>
      </div>
      <br>
   <% end %>
</div>
<br>
<% if current_user %>
   <p><%= link_to 'New Narrative', new_subtopic_narrative_path(@subtopic) %></p>
   <br>
<% end %>
<p><%= link_to 'Back', tcontainer_maintopic_path(@maintopic.tcontainer_id, @maintopic) %></p>



This my subtopics controller.
class SubtopicsController < ApplicationController
  # GET /subtopics
  # GET /subtopics.json
  #before_filter :load_forum

  before_filter :load_topic, :only => [:edit, :update, :show, :destroy]
  before_filter :load_maintopic, :only =>[:create, :index, :new]

  def index
     if current_user && current_user.admin?
        @subtopics = @maintopic.subtopics.all
     else
        render "public/404"
     end
  end

  # GET /subtopics/1
  # GET /subtopics/1.json
  def show
    #@subtopic.narratives.page(params[:page])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @subtopic }
    end
  end

  # GET /subtopics/new
  # GET /subtopics/new.json
  def new
     if current_user
        @user = current_user.id
        @subtopic = @maintopic.subtopics.build
        @subtopic.user_id = @user
     else
        redirect_to root_url
     end
  end

  # GET /subtopics/1/edit
  def edit
  end

  # POST /subtopics
  # POST /subtopics.json
  def create
     if current_user
        @user = current_user.id
        @subtopic = @maintopic.subtopics.new(params[:subtopic])
        @subtopic.user_id = @user
        if !(@subtopic.maintopic_id == @maintopic.id) #Prevents a subtopic from being assigned data to a maintopic that doesn't match
           redirect_to @maintopic
           return
        end

        @subtopic.created_on = Time.now
        @subtopic.save
        #if @subtopic.save
        #   redirect_to @maintopic.subtopic
        #else
        #   render "new";
        #end
        redirect_to maintopic_subtopic_path(@maintopic, @subtopic)
     else
        redirect_to root_url
     end
  end

  # PUT /subtopics/1
  # PUT /subtopics/1.json
  def update
    respond_to do |format|
      if @subtopic.update_attributes(params[:subtopic])
        format.html { redirect_to maintopic_subtopic_path(@subtopic.maintopic_id, @subtopic.id), notice: 'Subtopic was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @subtopic.errors, status: :unprocessable_entity }
      end
    end
  end
#raise
  # DELETE /subtopics/1
  # DELETE /subtopics/1.json
  def destroy
    @subtopic.destroy

    respond_to do |format|
      format.html { redirect_to tcontainer_maintopic_path(@maintopic.tcontainer_id, @maintopic.id) }
      format.json { head :no_content }
    end
  end
   private
      def load_topic
        @subtopic = Subtopic.find(params[:id])
        @maintopic = Maintopic.find(@subtopic.maintopic_id)
        @content = Maintopic.find(params[:maintopic_id])
        if @content.id != @maintopic.id
#           raise "I been tampered with and should redirect to the root page"
           redirect_to root_url
        end
      end

      def load_maintopic
         @maintopic = Maintopic.find(params[:maintopic_id])
      end
end
Eric
  • 25
  • 6

2 Answers2

0

in the Subtopic model add the string:

paginates_per 15

in the controller:

def index
  if current_user && current_user.admin?
    @subtopics = @maintopic.subtopics.page params[:page]
  else
    render "public/404"
  end
end

and in the index view add:

<%= paginate @subtopics %>

For more information about formatting paginator go to https://github.com/amatsuda/kaminari#helpers

Anton Grigoryev
  • 1,199
  • 11
  • 20
  • Thanks this is nice and all but the problem is my problem lies in indexing the show page contents rather then the index page. Otherwise this approach would work. I am trying to get narratives to be paginated from the show page. For example Subtopic Silverwing description a bat, Narrative one A dog. I want dog the narrative to be on the second page and the subtopic one to be on the first page. The paginates_per 15 I can give a try. – Eric May 03 '14 at 06:23
0

try this in your controller index

`@subtopics = kaminari.paginate_array(@subtopics).page(params[:page]).per(params[:per_page])`

and in your views, edit as

<%= page_entries_info @subtopics %> and also <%= paginate @subtopics, :theme => 'twitter-bootstrap-3' %>

Antony Mithun
  • 161
  • 1
  • 15
  • It looked like it could work, but I must be doing something wrong since I am getting an error. eric@ubuntu:~/Projects/Remote/LolaInterns/Trial$ rails s => Booting WEBrick => Rails 3.2.13 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server Exiting /home/eric/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/psych.rb:203:in `parse': (): mapping values are not allowed in this context at line 41 column 11 (Psych::SyntaxError) – Eric May 03 '14 at 15:10