我正在尝试将排序/重新排序添加到Active Admin中的资源。我知道您可以按不同的列排序,以便在登录时查看。我想要做的是能够订购项目,以便他们在前端以特定的顺序显示。对于如何实现这一点有什么想法吗?
我在数据库中已经有了一个排序列。
此外,我想显示在该特定的顺序上的项目在管理部分。
有没有人知道我该怎么做?
发布于 2012-01-20 10:49:18
我最近在我的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脚本(咖啡脚本)
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")发布于 2012-01-10 20:50:32
那么你有没有单独的前端控制器呢?对于你问题中的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祝好运。
https://stackoverflow.com/questions/8393455
复制相似问题