【问题标题】:Kaminari Issues in Rails 3.2Rails 3.2 中的 Kaminari 问题
【发布时间】:2014-05-03 23:25:31
【问题描述】:

如何让这个页面使用 kaminari 进行分页?我非常需要帮助。我不知道该怎么办。只要给我一些方法,这样所有的帖子每页只有 15 个。我希望显示页面分页而不是索引页面。

这是我建立的网站。 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

【问题讨论】:

  • 我添加了一些新的东西,并且仍然可以使用一些新的想法。我想让显示视图正常工作。

标签: ruby-on-rails ruby ruby-on-rails-3.2 pagination kaminari


【解决方案1】:

在 Subtopic 模型中添加字符串:

paginates_per 15

在控制器中:

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

并在索引视图中添加:

<%= paginate @subtopics %>

有关格式化分页器的更多信息,请访问https://github.com/amatsuda/kaminari#helpers

【讨论】:

  • 谢谢,这很好,但问题是我的问题在于索引显示页面内容而不是索引页面。否则这种方法会奏效。我正在尝试从显示页面对叙述进行分页。例如,子主题 Silverwing 描述一只蝙蝠,叙述一只狗。我希望将叙述放在第二页上,将副主题放在第一页上。 paginates_per 15 我可以试一试。
【解决方案2】:

在你的控制器索引中试试这个

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

在您的视图中,编辑为

&lt;%= page_entries_info @subtopics %&gt;&lt;%= paginate @subtopics, :theme =&gt; 'twitter-bootstrap-3' %&gt;

【讨论】:

  • 看起来它可以工作,但我一定做错了什么,因为我遇到了错误。 eric@ubuntu:~/Projects/Remote/LolaInterns/Trial$ rails s => 启动 WEBrick => Rails 3.2.13 应用程序开始在 0.0.0.0:3000 上开发 => 使用 -d 调用以分离 => Ctrl-C 以关闭服务器退出 /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这个上下文在第 41 行第 11 列 (Psych::SyntaxError)
猜你喜欢
  • 1970-01-01
  • 2011-10-08
  • 2014-04-18
  • 2013-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-01
相关资源
最近更新 更多