如何按时间顺序显示我的帖子? (最新到最旧)我试图在控制器的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
答案 0 :(得分:1)
@posts = @user.posts.order("updated_at DESC")
或
@posts = @user.posts.sort{|a,b| b.updated_at > a.updated_at}