簡體   English   中英

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

[英]How do I re-order table columns in a Rails app?

我在我的 Rails 應用程序中創建了一個模型,一段時間后意識到我忘記添加一些屬性,后來通過生成的遷移添加了它們。

我現在意識到schema.rb屬性列的順序是它們出現在 ActiveAdmin 中生成的資源視圖中的順序。

當我在 ActiveAdmin 中查看該模型時,我想對列重新排序,我認為這樣做的唯一方法是更改​​數據庫中的列順序。

我看過這里這里,並嘗試使用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 中的屬性列,我運行了數據庫遷移以更改列,但遷移並未對列重新排序。

您需要在 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

您可以在文檔中找到有關自定義索引顯示視圖的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM