Search code examples
ruby-on-railsdecent-exposure

How to make index view showing articles in descending order when using decent_exposure gem?


In decent_exposure gem doc there is an information, that we don't need to write down all standard controller actions (index, new, create, show, etc.), but we can only write create and update. In my app I have articles, and my goal is to put all articles in articles#index view, but in descending order, using created_at variable.

My controller:

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

Articles#index view:

- 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'

It doesn't work. Articles are printed out in ascending order. How to fix it?


Solution

  • Try expose(:articles) { Article.order('name DESC') }