如何在Rails应用中重新排序表列?

时间:2019-02-01 23:28:47

标签: ruby-on-rails database activeadmin schema.rb

我在Rails应用程序中创建了一个模型,一段时间后我意识到我忘记添加一些属性,后来又通过生成的迁移添加了它们。

我现在意识到schema.rb中属性列的顺序是它们在ActiveAdmin中生成的资源视图中出现的顺序。

我想在ActiveAdmin中查看模型时对列进行重新排序,而我唯一想到的方法是更改​​数据库中的列顺序。

我看过herehere,并尝试使用change_tablechange_column运行数据库迁移。没有任何变化。

这是我运行的迁移,没有结果:

class Reordercolumn < ActiveRecord::Migration[5.0]
  def up
    change_table :student_details do |t|
      t.change :exact_length, :text, after: :length_of_stay
      t.change :returned_home, :boolean, after: :spouse_name
      t.change :has_spouse, :boolean, after: :expectation
    end
  end
end

为了以特定顺序查看ActiveAdmin中的属性列,我进行了数据库迁移以更改列,但是迁移并未对列进行重新排序。

1 个答案:

答案 0 :(得分:1)

您需要对ActiveAdmin中的列重新排序吗?让我们在相应的admin/student_detail.rb文件

中进行操作
index do
  selectable_column
  column :exact_length
  column :returned_home
  column :has_spouse
end

show do
  attributes_table do
    row :title
    row :returned_home
    row :has_spouse
  end
end

有关自定义indexshow视图的更多信息,您可以在文档中找到