ruby-on-railsrubyruby-on-rails-4impressionist

Link to Order Index Rails 4


I am using Rails 4 with the Impressionist gem to get view count of my Articles.

On my index page I have a link labeled "Most Popular"

I also have access to a method that will order articles by the view count:

@articles = Article.order('impressions_count ASC')

What is the best way to order the index by impression_count when a user clicks the "most popular button?" I am having trouble finding documentation on this.

Here is my articles_controller.rb

class ArticlesController < ApplicationController
  load_and_authorize_resource
  before_action :set_article, only: [:show, :edit, :update, :destroy]

  def index
    if params[:q].present?
       @articles = Article.search(params[:q], misspellings: {edit_distance: 1}, page: params[:page], per_page: 12)
       @search = current_or_guest_user.searches.create!(query: params[:q], searched_at: Time.now)
    else
       @articles = Article.order('impressions_count ASC').page(params[:page]).per(12)
       @search = current_or_guest_user.searches.create!(query: params[:q], searched_at: Time.now)
    end
    if @articles.blank?
       return redirect_to request_path
       @search = current_or_guest_user.searches.create!(query: params[:q], searched_at: Time.now)
    end
    get_query
  end

  def autocomplete
    @articles = Article.search params[:term], autocomplete: true
    render json: @articles
  end

  def search
    @articles = Article.search params[:q], suggest: true, page: params[:page], per_page: 5
    @search = current_or_guest_user.searches.create!(query: params[:q], searched_at: Time.now)
    render 'index'
  end

  def show
    impressionist(@article, nil, { unique: [:session_hash] })
    @skip_error = true
    @subarticles = @article.subarticles.approved.order(:cached_votes_score => :desc)
   if request.path != article_path(@article)
      redirect_to @article, status: :moved_permanently
    else
        respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @article }
      end
    end
  end

  def new
  end

  def edit
  end

  def create
     respond_to do |format|
      if @article.save
        format.html { redirect_to @article, notice: 'Article was successfully created.' }
        format.json { render :show, status: :created, location: @article }
      else
        format.html { render :new }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @article.update(article_params)
        format.html { redirect_to @article, notice: 'Article was successfully updated.' }
        format.json { render :show, status: :ok, location: @article }
      else
        format.html { render :edit }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @article.destroy
    respond_to do |format|
      format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_article
      @article = Article.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def article_params
      params.require(:article).permit(:title, :specific, :category, :aka, :image1, :image2, :video1, :video2)
    end

    def get_query
      @userquery = params[:q]
    end
end

Solution

  • The first else clause in your index is this:

    else
       @articles = Article.order('impressions_count ASC').page(params[:page]).per(12)
    

    That is why you are getting them sorted by impressions_count. Just get rid of it to return them sorted by most recent.

    else
       @articles = Article.order(created_at: :desc).page(params[:page]).per(12)
    

    You then need to set your "most popular" link to return the @articles variable sorted by impressions_count as you did in your old code.

    You will need to make your action in the controller to return the results you want, something like:

    Add a variable to your whitelist:

    def article_params
      params.require(:article).permit(:title, :specific, :category, :aka, :image1, :image2, :video1, :video2, :imp_sort)
    end
    

    Then in your index action you can add it in:

     def index
    if params[:q].present?
       @articles = Article.search(params[:q], misspellings: {edit_distance: 1}, page: params[:page], per_page: 12)
       @search = current_or_guest_user.searches.create!(query: params[:q], searched_at: Time.now)
    else
       if article_params[:imp_sort]
         @articles = Article.order('impressions_count ASC').page(params[:page]).per(12)
       else
         @articles = Article.order(created_at: :desc).page(params[:page]).per(12)
       end
       @search = current_or_guest_user.searches.create!(query: params[:q], searched_at: Time.now)
    end
    if @articles.blank?
       return redirect_to request_path
       @search = current_or_guest_user.searches.create!(query: params[:q], searched_at: Time.now)
    end
    get_query
    

    end

    in your index.html.erb you will need to have your link do something like:

    <%= link_to "Sort by Impressions", articles_path(:imp_sort => true) %>