Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何让我的帖子按时间顺序显示?(从最新到最老)_Ruby On Rails_Ruby On Rails 4_View_Controller - Fatal编程技术网

Ruby on rails 如何让我的帖子按时间顺序显示?(从最新到最老)

Ruby on rails 如何让我的帖子按时间顺序显示?(从最新到最老),ruby-on-rails,ruby-on-rails-4,view,controller,Ruby On Rails,Ruby On Rails 4,View,Controller,如何让我的帖子按时间顺序显示?(从最新到最旧)我尝试在控制器的show部分中添加'all.order(“created_at DESC”).paginate(:page=>params[:page]),但是,它不起作用。有什么想法吗?我知道我在这里遗漏了一些非常简单的东西。提前感谢 User/Show.html <div id="posts" class="transitions-enabled"> <% @posts.each do |post| %>

如何让我的帖子按时间顺序显示?(从最新到最旧)我尝试在控制器的show部分中添加'all.order(“created_at DESC”).paginate(:page=>params[:page]),但是,它不起作用。有什么想法吗?我知道我在这里遗漏了一些非常简单的东西。提前感谢

User/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}