2015-05-28 56 views
0

我使用Rails 4与印象派宝石来获得我的文章的查看次数。链接到订单索引轨道4

在我的索引页我有一个标有“最受欢迎”的链接

我也有机会获得,将责令被观看次数物品的方法:

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

什么是最好的方式当用户点击“最流行的按钮”时,按impression_count排序索引?我无法找到关于此的文档。

这里是我的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 
+0

你是什么意思只订购索引? – Zakwan

+0

当有人点击“最受欢迎”链接时,我想按impression_count进行排序 – Kathan

+0

只是文章显示页面。这个方法'印象派(@article,nil,{unique:[:session_hash]})'获得文章的查看次数。 – Kathan

回答

1

索引中的第一个else条款是这样的:

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

这就是为什么你让他们通过impressions_count排序。只是摆脱它返回他们排序最近。

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

然后你需要设置你的“最流行的”链接返回@articles变量通过impressions_count排序为你在你的旧代码一样。

你需要让你的动作控制器返回你想要的结果,是这样的:

一个变量添加到您的白名单:

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

然后在你的index动作你可以添加它在:

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 

在你的index.html.erb你需要让你的链接做类似:

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

这正是我一直在寻找的,非常感谢。 – Kathan