使用 decent_exposure gem 时如何使索引视图按降序显示文章?
How to make index view showing articles in descending order when using decent_exposure gem?
在 decent_exposure gem 文档中有一条信息,我们不需要写下所有标准控制器操作(索引、新建、创建、显示等),但我们只能写create和update。
在我的应用程序中,我有文章,我的目标是将所有文章放在 articles#index 视图中,但使用 created_at 变量按降序排列。
我的控制器:
class ArticlesController < ApplicationController
expose(:articles)
expose(:article, attributes: :article_params)
def index
articles = Article.all.order('created_at DESC')
end
def create
if article.save
redirect_to article_path(article)
else
render 'new'
end
end
def update
if article.save
redirect_to article_path(article)
else
render 'edit'
end
end
def destroy
if article.destroy
redirect_to articles_path
else
render 'show'
end
end
private
def article_params
params.require(:article).permit(:title, :head, :content)
end
end
文章#index 视图:
- articles.each do |article|
%h4
= link_to article.title, article
%h6
= link_to article.head, article
= link_to 'Add article', new_article_path, class: 'btn btn-success'
没用。文章按升序打印。如何解决?
尝试expose(:articles) { Article.order('name DESC') }
在 decent_exposure gem 文档中有一条信息,我们不需要写下所有标准控制器操作(索引、新建、创建、显示等),但我们只能写create和update。 在我的应用程序中,我有文章,我的目标是将所有文章放在 articles#index 视图中,但使用 created_at 变量按降序排列。
我的控制器:
class ArticlesController < ApplicationController
expose(:articles)
expose(:article, attributes: :article_params)
def index
articles = Article.all.order('created_at DESC')
end
def create
if article.save
redirect_to article_path(article)
else
render 'new'
end
end
def update
if article.save
redirect_to article_path(article)
else
render 'edit'
end
end
def destroy
if article.destroy
redirect_to articles_path
else
render 'show'
end
end
private
def article_params
params.require(:article).permit(:title, :head, :content)
end
end
文章#index 视图:
- articles.each do |article|
%h4
= link_to article.title, article
%h6
= link_to article.head, article
= link_to 'Add article', new_article_path, class: 'btn btn-success'
没用。文章按升序打印。如何解决?
尝试expose(:articles) { Article.order('name DESC') }