频道栏目
首页 > 程序开发 > 综合编程 > 其他综合 > 正文
ActiveAdmin 自定义表单
2015-10-15 13:40:33      个评论    来源:右撇子  
收藏   我要投稿

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、类别 :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

5、数据集 :collection
配合 :as => :check_boxes、:as => :select、:as => :radio、:as => :datalist使用。

 

点击复制链接 与好友分享!回本站首页
相关TAG标签 表单
上一篇:ActiveAdmin 数据管理
下一篇:Thread、Handler与HandlerThread
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站