【问题标题】:How do I re-order table columns in a Rails app?如何在 Rails 应用程序中重新排序表列?
【发布时间】:2019-02-01 23:28:47
【问题描述】:

我在我的 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 中的属性列,我运行了数据库迁移以更改列,但迁移并未对列重新排序。

【问题讨论】:

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


    【解决方案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 视图的更多信息

    【讨论】:

      猜你喜欢
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 2019-09-18
      相关资源
      最近更新 更多