2013-03-16 60 views
2

默認情況下,我的模型關聯顯示在rails_admin的「添加新」選項卡中。但是,如果我修改rails_admin.rb文件並使用config.model來自定義視圖中顯示的字段,那麼顯然會將其從視圖中移除。在rails_admin的視圖中顯示模型關聯

在我的例子中,我有一個Customer和一個User。

User.rb

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :role,  :company, :customer_id 

    belongs_to :customer 
end 

Customer.rb

class Customer < ActiveRecord::Base 
    attr_accessible :city, :name, :state, :street, :zip, :container_id, :user_id 

    has_many :users 
end 

所以,現在如果我登錄到rails_admin儀表板和去添加新的用戶,我有一個droppdown選項來選擇客戶,添加新客戶的按鈕以及編輯客戶的按鈕。但是,一旦我在rails_admin.rb中添加我的config.model。

rails_admin.rb

config.model 'User' do 
     list do 
     field :name do 
      label "Name" 
     end 
     field :email do 
      label "Email" 
     end 
     field :company do 
      label "Company" 
    end 
    field :role do 
     label "Role" 
    end 
    end 
    edit do 
    field :name do 
     label "Name" 
    end 
    field :email do 
     label "Email" 
    end 
    field :company do 
     label "Company" 
    end 
    field :role do 
     label "Role" 
    end 
    field :role, :enum do 
     enum do 
      ['1a', '1b', '2a', '2b', '3a', '3b', '4a', '4b', '5'] 
     end 
     label "Role" 
    end 
    end 
    create do 
    field :name do 
     label "Name" 
    end 
    field :email do 
     label "Email" 
     help "The email address will serve as the username." 
    end 
    field :password do 
     label "Password" 
    end 
    field :password_confirmation do 
     label "Password Confirmation" 
     help "Required" 
    end 
    field :company do 
     label "Company" 
    end 
    field :role, :enum do 
     enum do 
      ['1a', '1b', '2a', '2b', '3a', '3b', '4a', '4b', '5'] 
     end 
     label "Role" 
    end 
    end 
    end 

這將覆蓋時,我想添加一個用戶,我有關聯的字段。我的問題是,我應該在rails_admin中使用什麼語法來明確告訴rails_admin配置,我希望在我去用rails_admin添加用戶時顯示模型關聯。

謝謝!

回答

3

在你rails_admin.rb文件,其中是您的自定義配置塊,你只需要聲明這樣的聯想:

config.model 'User' do 
    configure :customer, :belongs_to_association 
... 
... 

,並使用上述鍵字段名:

edit do 
    field :customer do 
    label "Customer" 
    end 
end 
+0

'配置:customer,:belongs_to_association'對於rails_admin 0.6.7 btw不是必需的 – 2015-06-17 16:31:41

相關問題