我正在尝试向Active Admin中的资源添加排序/重新排序。我了解您可以按登录时的不同列进行排序。我想要做的是能够订购商品,以便在前端按特定顺序显示。关于如何实现这一目标的任何想法?
我已经在数据库中有一个排序列。
此外,我还想在管理部分中按特定顺序显示项目。
任何人对我如何实现这一点都有任何想法?
答案 0 :(得分:25)
我最近在HomeSlide模型上使用名为position的列实现了这一点。
ActiveAdmin.register HomeSlide do
config.sort_order = 'position_asc'
index do
column :title
default_actions
end
# This action is called by javascript when you drag and drop a column
# It iterates through the collection and sets the new position based on the
# order that jQuery submitted them
collection_action :sort, :method => :post do
params[:home_slide].each_with_index do |id, index|
HomeSlide.update_all(['position=?', index+1], ['id=?', id])
end
render :nothing => true
end
end
将此添加到您的active_admin javascripts(咖啡脚本)
sendSortRequestOfModel = (model_name) ->
formData = $('#' + model_name + ' tbody').sortable('serialize')
formData += "&" + $('meta[name=csrf-param]').attr("content") + "=" + encodeURIComponent($('meta[name=csrf-token]').attr("content"))
$.ajax
type: 'post'
data: formData
dataType: 'script'
url: '/admin/' + model_name + '/sort'
jQuery ($) ->
# home page slides
if $('body.admin_home_slides.index').length
$( "#home_slides tbody" ).disableSelection()
$( "#home_slides tbody" ).sortable
axis: 'y'
cursor: 'move'
update: (event, ui) ->
sendSortRequestOfModel("home_slides")
答案 1 :(得分:0)
那么你有一个独立的前端控制器吗?对于问题的activeadmin部分:
可以有一个
config.sort_order = 'lastname_asc'
或
config.sort_order = 'created_at_desc'
模型/资源的ActiveAdmin.register块中的声明。
在你的模型中,你可以输入类似
的东西default_scope :order => "id DESC"
阅读您的问题,虽然您似乎有必要阅读一些文档,这是从http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html复制的部分
class Article < ActiveRecord::Base
scope :published, where(:published => true)
scope :featured, where(:featured => true)
def self.latest_article
order('published_at desc').first
end
def self.titles
map(&:title)
end
end
允许您调用这样的方法:
Article.published.featured.latest_article
Article.featured.titles
祝你好运。