在rails_admin的视图中显示模型关联

时间:2013-03-16 20:44:18

标签: ruby-on-rails rails-admin

默认情况下,我的模型关联会显示在rails_admin的“添加新”标签中。但是,如果我修改rails_admin.rb文件并使用config.model来自定义视图中显示的字段,它显然会从视图中删除它。

在我的例子中,我有一个客户和一个用户。

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选项来选择Customer,一个按钮来添加一个新客户,一个按钮来编辑一个客户。但是,一旦我在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中添加用户时显示模型关联。

谢谢!

1 个答案:

答案 0 :(得分:3)

rails_admin.rb文件中,您的自定义配置块在哪里,您只需要声明这样的关联:

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

并使用上面的键作为字段名称:

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