使用活动管理员删除索引视图中的列
Remove column in index view using active admin
这里我使用了 active admin 和 rails 6. 考虑索引视图有将近 5 到 10 列。如果我想自定义列意味着应该这样做。
index do
column :name
column :description
column :released_year
column :director
column :producer
column :artist
actions
end
好的。没问题。
除非,我的模型有近50列。我想显示 48 列。当时我想描述这48列应该显示。我的问题是,我们是否必须从索引视图中删除这两列,而不是编写必要的列。喜欢,
index do
remove_column :created_at
remove_column :updated_at
end
如果您的模型名为模型,请尝试:
attributes_to_display = Model.new.attributes.keys - ['attribute_1', 'attribute_2']
index do
attributes_to_display.each do |attribute|
column attribute.to_sym
end
actions
end
resource_columns 为您处理关联,因此:
attributes = active_admin_config.resource_columns - [:attribute_1, :attribute_2]
index do
selectable_column
id_column
attributes.each do |attribute|
column attribute
end
actions
end
这对我有用(MyModel
beeing your Model):
exclude_columns = [:attribute_to_exclude_1, :attribute_to_exclude_2]
index do
selectable_column
ModelName.attribute_names.each do |clmn|
column clmn if not exclude_columns.include? clmn.to_sym
end
actions
end
上述解决方案对我有用,但它没有显示与 link 相关联的对象,只是显示了相关联的对象 ID。在我的例子中,我只有一个关联对象,所以我这样处理它
index do
columns_to_exclude = ["url"]
(Feature.column_names - columns_to_exclude).each do |c|
if c != "car_model_id"
column c.to_sym
else
column "car_model".to_sym
end
end
通过这种方式,您的关联对象行为得以保留并且您可以从 link.
转到父对象
这里我使用了 active admin 和 rails 6. 考虑索引视图有将近 5 到 10 列。如果我想自定义列意味着应该这样做。
index do
column :name
column :description
column :released_year
column :director
column :producer
column :artist
actions
end
好的。没问题。
除非,我的模型有近50列。我想显示 48 列。当时我想描述这48列应该显示。我的问题是,我们是否必须从索引视图中删除这两列,而不是编写必要的列。喜欢,
index do
remove_column :created_at
remove_column :updated_at
end
如果您的模型名为模型,请尝试:
attributes_to_display = Model.new.attributes.keys - ['attribute_1', 'attribute_2']
index do
attributes_to_display.each do |attribute|
column attribute.to_sym
end
actions
end
resource_columns 为您处理关联,因此:
attributes = active_admin_config.resource_columns - [:attribute_1, :attribute_2]
index do
selectable_column
id_column
attributes.each do |attribute|
column attribute
end
actions
end
这对我有用(MyModel
beeing your Model):
exclude_columns = [:attribute_to_exclude_1, :attribute_to_exclude_2]
index do
selectable_column
ModelName.attribute_names.each do |clmn|
column clmn if not exclude_columns.include? clmn.to_sym
end
actions
end
上述解决方案对我有用,但它没有显示与 link 相关联的对象,只是显示了相关联的对象 ID。在我的例子中,我只有一个关联对象,所以我这样处理它
index do
columns_to_exclude = ["url"]
(Feature.column_names - columns_to_exclude).each do |c|
if c != "car_model_id"
column c.to_sym
else
column "car_model".to_sym
end
end
通过这种方式,您的关联对象行为得以保留并且您可以从 link.
转到父对象