在Rails中显示最新的博客文章

时间:2015-06-17 23:09:35

标签: ruby-on-rails ruby

  

TL; DR如何实现此行为?是否有其他方法可以特别使用?

我有一个具有博客功能的Rails应用程序。我试图按降序显示所有最近的帖子,但没有运气。以下是我的消息来源:

应用程序/控制器/ blog_controller.rb

class BlogController < ApplicationController
 before_filter :authenticate_user!, except: [:index, :show]
 before_filter :set_blog, only: [:show, :edit, :update, :destroy]

def index
  @blog = Blog.all
end

def new
  @blog = Blog.new
end

def create
  @blog = Blog.new(blog_params)

 redirect_to_blog_on(@blog.save, :error_template => :new, :notice => 'Post successfully posted.')   
end

def update

  redirect_to_blog_on(@blog.update(blog_params), :error_template => :edit, :notice => 'Post updated successfully.')
end

def destroy
  @blog.destroy

  respond_to do |format|
   format.html { redirect_to blog_index_url, :notice => 'Post deleted successfully.' }
 end
end

private

def set_blog
  @blog = Blog.find(params[:id])
end

def blog_params
  params.require(:blog).permit(:title, :content)
end

def redirect_to_blog_on(condition, error_template: nil, notice: '')
  respond_to do |format|
    if condition
      format.html { redirect_to @blog, notice: notice }
      format.json { render :show, status: :created, location: @blog }
    else
      format.html { render error_template }
      format.json { render json: @blog.errors, status: :unprocessable_entity }
     end
   end
  end
 end

在show.html.erb上,我有一个标题和正文内容,我想我可以做一些Latest Posts代码,但它让我痛苦地失败了。

应用程序/视图/博客/ show.html.erb

<div class="container">
  <h1><%= @blog.title %><hr></h1>
<div class="row">
    <div class='col-md-4'>
        <p><i class="glyphicon glyphicon-time"></i> Date Posted: <%= @blog.created_at.to_s(:long) %></p>
    </div>
    <div class="col-md-offset-0">
        <p><% if @blog.created_at != @blog.updated_at %><i class="glyphicon glyphicon-ok"></i> Updated at: <%= @blog.updated_at.to_s(:long) %><% end %></p>
    </div>
</div>
<br>
<div class="row">
    <div class="col-lg-7">
    <p><%= markdown @blog.content %></p>
    <div class="col-md-3">
        <% if user_signed_in? %>
            <%= link_to edit_blog_path(@blog) do %><i class="glyphicon glyphicon-edit"></i> Edit<% end %>
        <% end %>
    </div>
    <div class="col-md-3">
        <% if user_signed_in? %>
            <%= link_to blog_path(@blog), method: :delete, data: { confirm: 'Are you sure?' } do %><i class="glyphicon glyphicon-trash"></i> Delete Post<% end %>
        <% end %>
    </div>
    </div>
    <div class="col-md-offset-8">
        <h4>Latest Posts</h4>
        <% if @blog.present? %>
         <% @blogs.each do |blog| %>
            <%= link_to blog_path(blog) do %><%= blog.title %><% end %>
          <% end %>
        <% else %>
        <h3>Check back later!</h3>
        <% end %>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

按照从数据库中检索时创建的日期订购博客帖子。以下是index操作的外观:

def index
  @blog = Blog.order("created_at DESC")
end

这将按照创建日期的降序从数据库中检索所有博客帖子。最新的帖子将在数组的开头(@blog[0]),而最旧的帖子将在数组的最后。

我建议你复制实例变量名,这样你就知道它是一个数组:

def index
  @blogs = Blog.order("created_at DESC")
end

有关使用Active Record订购的更多详细信息,请查看此link

相关问题