ActiveAdmin自定义属地表单 [英] ActiveAdmin customizing the form for belongs_to
问题描述
我有以下关联:
class Course < ActiveRecord::Base
has_many :signup
has_many :user, :through => :signup
accepts_nested_attributes_for :signup
end
class User < ActiveRecord::Base
has_many :signup
has_many :course, :through => :signup
accepts_nested_attributes_for :signup
end
class Signup < ActiveRecord::Base
belongs_to :course
belongs_to :user
end
现在,我想为注册自定义ActiveAdmin表单,以便将课程标题和用户名显示为选择内容而不是文本字段。
Now, I would like to customize the ActiveAdmin form for "Signup", so it shows the title of the courses and the name of the users as a select and not as a textfield.
默认表单已经做到了,但是我需要进一步自定义表单,因此无法重现默认表单。
The default form already does that, however I need to customize the form further and I can't reproduce the default form.
推荐答案
您的表单块将在您的 admin / signups.rb
中看起来像这样:
Your form block will look something like this in your admin/signups.rb
:
form do |f|
f.input :course
t.input :user
end
默认情况下,由于课程
和 user
是关联,因此应该为您提供 collection_select
-即,将模型的 name
属性作为标签, id
s作为价值。如果您已通过输入
sa输入类型,这将迫使它们显示为该类型。
By default, since course
and user
are associations, this should give you a collection_select
- that is, a with the name
attribute of your models as labels, id
s as values. If you had passed your input
s a input type, this will force them to display as that type.
form do |f|
f.input :course, :as => :string
end
这只会为您提供 course_id
文本输入字段,您可能仅在其中输入关联对象的ID。要重现默认格式,只需继续为相关属性添加 input
s。您甚至可以将它们包装在 f.inputs
中进行分组,使它们看起来很漂亮。
This'll just give you a course_id
text input field, where you'll probably just enter the ID for the associated object. To "reproduce the default form", just continue adding input
s for the relevant attributes. You can even wrap them in f.inputs
to group them and make things look pretty.
form do |f|
f.inputs "Basic Details" do
f.input :course
f.input :user
end
end
这篇关于ActiveAdmin自定义属地表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!