Showing model associations in the views of rails
By default my model associations show up in the 'Add New' tab of rails_admin. However, if I modify the rails_admin.rb file and use config.model to customize what fields appear in the view, it obviously will remove it from the view.
In my example, I have a Customer and a 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
So now if I log in to the rails_admin dashboard and go to add a new user, I have a droppdown option to select a Customer, a button to add a new customer, and a button to edit a customer. However, once I add my config.model in rails_admin.rb.
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
This will override the association fields that I have when I want to add a user. My question is what is the syntax I should use in rails_admin to explicitly tell the rails_admin config that I would like the model association to show up when I go to add a user in rails_admin.
Thanks!
In your rails_admin.rb
file, where is it your custom configuration block, you just need declare the association like this:
config.model 'User' do
configure :customer, :belongs_to_association
...
...
And use the above key as field name:
edit do
field :customer do
label "Customer"
end
end
链接地址: http://www.djcxy.com/p/67052.html
上一篇: 红宝石在轨道上
下一篇: 在rails的视图中显示模型关联