簡體   English   中英

如何使我的帖子按時間順序顯示? (最新到最舊)

[英]How can I get my post to display in chronological order? (newest to oldest)

如何使我的帖子按時間順序顯示? (從最新到最舊)我嘗試在控制器的show部分中添加'all.order(“ created_at DESC”)。paginate(:page => params [:page]'),但它不起作用。知道我在這里很容易遺漏一些東西,謝謝!

用戶/ Show.html

<div id="posts" class="transitions-enabled">
      <% @posts.each do |post| %>

        <div class="box panel panel-default">
          <%= link_to image_tag(post.image.url(:medium)), post %>
          <div class="panel-body">
          <%= post.description %><br/>
          <strong><%= post.user.name if post.user %></strong>

          <% if post.user == current_user %>
            <div class="actions">
            <%= link_to edit_post_path(post) do %>
            <span class="glyphicon glyphicon-edit"></span>
                Edit
              <% end %>
            <%= link_to post, method: :delete, data: { confirm: 'Are you sure?' } do %>
            <span class="glyphicon glyphicon-trash"></span>
                Delete
              <% end %>
           </div>
          <% end %>
        </div>
       </div> 
   <% end %>
  </div>

用戶/控制器

def index
    @users = User.paginate(page: params[:page], :per_page => 20)
  end


  def show
    @user = User.find(params[:id])

    if @user 

        @posts = @user.posts.all
        render actions: :show
    else
        render file: 'public/404', status: 404, formats: [:html]
    end  

def destroy
    User.find(params[:id]).destroy
    flash[:success] = "Your account has been deleted."
    redirect_to root_path
  end

  def correct_user
      @user = User.find(params[:id])
      redirect_to root_path 
    end

  def admin_user
      redirect_to root_path unless current_user.admin?
  end


  end
end
@posts = @user.posts.order("updated_at DESC")

要么

 @posts = @user.posts.sort{|a,b| b.updated_at > a.updated_at}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM