2013-03-16 64 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

相关问题