您的位置:首页 > 其它

ActiveAdmin 自定义表单

2015-10-14 12:38 561 查看

ActiveAdmin 自定义表单

ActiveAdmin的表单定义,用的是Formtastic

基础示例

ActiveAdmin.register Post do

form do |f|
f.inputs 'Details' do
f.input :title
f.input :published_at, label: 'Publish Post At'
end
f.actions
end

end


f.inputs 会显示一个Panel,上面将显示 f.inputs 内定义的所有元素。

f.input 一个输入元素,类似的还有:f.action 操作按钮。

f.actions 操作集合,里面可以定义一个或多个 f.action。

input类型

f.input可以定义多种类型,如check_boxes、select、radio、datalist。

用法:

f.input :authors, :as => :check_boxes, :collection => User.order("last_name ASC").all
f.input :authors, :as => :check_boxes, :collection => current_user.company.users.active
f.input :authors, :as => :check_boxes, :collection => [@justin, @kate]
f.input :authors, :as => :check_boxes, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
f.input :author,  :as => :select,      :collection => Author.all
f.input :author,  :as => :select,      :collection => Author.pluck(:first_name, :id)
f.input :author,  :as => :select,      :collection => Author.pluck(Arel.sql("CONCAT(`first_name`, ' ', `last_name`)"), :id)
f.input :author,  :as => :select,      :collection => Author.your_custom_scope_or_class_method
f.input :author,  :as => :select,      :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
f.input :author,  :as => :select,      :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
f.input :author,  :as => :radio,       :collection => User.all
f.input :author,  :as => :radio,       :collection => [@justin, @kate]
f.input :author,  :as => :radio,       :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
f.input :author,  :as => :radio,       :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
f.input :admin,   :as => :radio,       :collection => ["Yes!", "No"]
f.input :book_id, :as => :select,      :collection => Hash[Book.all.map{|b| [b.name,b.id]}]
f.input :fav_book,:as => :datalist   , :collection => Book.pluck(:name)


更多设置

1、设置标签 :label

f.input :categories, :label => "类别"


2、是否必填 :required

f.input :categories, :required => false


3、备注,默认显示在输入框下面 :hint

f.input :categories, :hint => "这里填您的身份证号码噢"


4、设置默认值

f.input :title, :input_html => {:value => "这是标题"}


5、隐藏域

f.input :post_id,:as=>:hidden, :input_html => {:value => "10010"}


6、完整类型列表 :as

:select – a select menu. Default for ActiveRecord associations: belongs_to, has_many, and has_and_belongs_to_many.

:check_boxes – a set of check_box inputs. Alternative to :select for ActiveRecord-associations: has_many, and has_and_belongs_to_many.

:radio – a set of radio inputs. Alternative to :select for ActiveRecord-associations: belongs_to.

:time_zone – a select input. Default for column types: :string with name matching “time_zone”.

:password – a password input. Default for column types: :string with name matching “password”.

:text – a textarea. Default for column types: :text.

:date_select – a date select. Default for column types: :date.

:datetime_select – a date and time select. Default for column types: :datetime and :timestamp.

:time_select – a time select. Default for column types: :time.

:boolean – a checkbox. Default for column types: :boolean.

:string – a text field. Default for column types: :string.

:number – a text field (just like string). Default for column types: :integer, :float, and :decimal.

:file – a file field. Default for file-attachment attributes matching: paperclip or attachment_fu.

:country – a select menu of country names. Default for column types: :string with name “country” – requires a country_select plugin to be installed.

:email – a text field (just like string). Default for columns with name matching “email”. New in HTML5. Works on some mobile browsers already.

:url – a text field (just like string). Default for columns with name matching “url”. New in HTML5. Works on some mobile browsers already.

:phone – a text field (just like string). Default for columns with name matching “phone” or “fax”. New in HTML5.

:search – a text field (just like string). Default for columns with name matching “search”. New in HTML5. Works on Safari.

:hidden – a hidden field. Creates a hidden field (added for compatibility).

:range – a slider field.

:datalist – a text field with a accompanying datalist tag which provides options for autocompletion

7、数据集 :collection

配合 :as => :check_boxes、:as => :select、:as => :radio、:as => :datalist使用。

参考资料

https://github.com/justinfrench/formtastic

http://activeadmin.info/docs/5-forms.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: