为will_paginate + order_by参数创建link_to操作(Rails)

时间:2012-02-09 13:06:17

标签: ruby-on-rails-3

我的评论中有一个名为total_votes的参数(由整数组成)。 这就是我通过投票最多的方式订购评论并同时申请分页的方式:

show.rb:

  def show
    @post = Post.find(params[:id])
    @comments = @post.comments.paginate(:page => params[:page],
                                        :per_page => 5).order('total_votes DESC,
                                                               created_at DESC')
  end

show.html.erb:

<% @comments.map do |comment| %>
  <div class="comment-<%= comment.id %>">
  <p>
    <b>Comment:</b>
    <%= comment.content %>
  </p>
  <p>
    <b>Vote:</b>
    <span class="vote-count"><%= comment.total_votes %></span>

    <div class='voted-user'>
      <% comment.votes.each do |vote| %>
        <%= link_to vote.user.username, vote.user %>
      <% end %>
    </div>
  </p>
  <p>
    <b>Commenter</b>
    <%= link_to comment.user.username, comment.user %>
  </p>
  <p>
    <b>Link</b>
    <%= link_to "Show Post Comment", [@post, comment] %>
  </p>
  <p>
    <b>Vote</b>
    <%= link_to "Vote Up", vote_up_path(comment, :votable_type => "Comment"), :remote => true, :class => "vote-up" %><br />
  </p>
  </div>
<% end %>

<%= will_paginate @comments %>

我想创建一个link_to动作,以便我可以操作这部分:

@comments = @post.comments.paginate(:page => params[:page],
                                    :per_page => 5).order('THIS_PART')

这样我就可以存档这样的内容:

  

mysite的/帖/ 93?ORDER_BY =票

和/或(order_by + will_paginate):

  

mysite的/帖/ 93 ORDER_BY =票&安培;页= 2

或类似的东西。

有关如何在控制器和视图中完成此代码的任何建议(如果这不是最好的方法,请告诉我)?

1 个答案:

答案 0 :(得分:1)

编辑:

Railscasts就是你想要的!