Link 至订单索引 Rails 4
Link to Order Index Rails 4
我正在使用 Rails 4 和印象派 gem 来获取我的文章的浏览量。
在我的索引页上,我有一个 link 标记为 "Most Popular"
我还可以访问一种按浏览次数排序文章的方法:
@articles = Article.order('impressions_count ASC')
当用户单击 "most popular button?" 时,按 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
索引中的第一个 else
子句是这样的:
else
@articles = Article.order('impressions_count ASC').page(params[:page]).per(12)
这就是为什么要按 impressions_count 对它们进行排序的原因。只需删除它即可 return 它们按最近排序。
else
@articles = Article.order(created_at: :desc).page(params[:page]).per(12)
然后您需要将 "most popular" link 设置为 return @articles
变量按 impressions_count 排序,就像您在旧代码中所做的那样。
您需要在控制器中执行操作以 return 您想要的结果,例如:
将变量添加到您的白名单:
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 执行如下操作:
<%= link_to "Sort by Impressions", articles_path(:imp_sort => true) %>
我正在使用 Rails 4 和印象派 gem 来获取我的文章的浏览量。
在我的索引页上,我有一个 link 标记为 "Most Popular"
我还可以访问一种按浏览次数排序文章的方法:
@articles = Article.order('impressions_count ASC')
当用户单击 "most popular button?" 时,按 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
索引中的第一个 else
子句是这样的:
else
@articles = Article.order('impressions_count ASC').page(params[:page]).per(12)
这就是为什么要按 impressions_count 对它们进行排序的原因。只需删除它即可 return 它们按最近排序。
else
@articles = Article.order(created_at: :desc).page(params[:page]).per(12)
然后您需要将 "most popular" link 设置为 return @articles
变量按 impressions_count 排序,就像您在旧代码中所做的那样。
您需要在控制器中执行操作以 return 您想要的结果,例如:
将变量添加到您的白名单:
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 执行如下操作:
<%= link_to "Sort by Impressions", articles_path(:imp_sort => true) %>