ActiveAdmin 自定义表单

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值