它目前列出我最古老的文章在頂部,我想做相反的事情。我想我需要將它命名爲created_at,但我還沒有完成它的工作。我知道這很容易,但我仍然是一個新手。由於呈現最近的文章軌道4
目前我有
<div class="bit-75">
<h2 id="title"><%= link_to article.title, article_path(article) %></h2>
<br>
<ul id="article-links">
<div id="article-image"><%= image_tag article.image_url %></div>
<br>
<li id="article-text"><%= article.text %></li>
<br>
<%= article.created_at %>
<br>
<% if admin_signed_in? %>
<li><%= link_to 'Edit', edit_article_path(article) %></li>
<li><%= link_to 'Destroy', article_path(article),
method: :delete, data: { confirm: 'Are you sure?'} %></li>
<li><%= link_to 'New article', new_article_path %></li>
<% else %>
<li><%= link_to 'Make a Comment', article_path(article) %></li>
</ul>
<% end %>
article.rb
class Article < ActiveRecord::Base
has_many :comments, dependent: :destroy
validates :title, presence: true,
length: { minimum: 5 }
mount_uploader :image, ImageUploader
end
文章控制器
def new
@article = Article.new
end
def index
@article = Article.all
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def show
@article = Article.find(params[:id])
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
嘗試索引操作中的Article.order(「created_at DESC」)' –
謝謝Shamsul。我早些時候嘗試過,但我認爲我犯了一個語法錯誤,但沒有奏效。 default_scope確實有效。謝謝您的幫助。 –