ActiveAdmin 自定义表单

ActiveAdmin 自定义表单

ActiveAdmin的表单定义,,用的是Formtastic

基础示例ActiveAdmin.register Post do form do |f|f.inputs ‘Details’ dof.input :titlef.input :published_at, label: ‘Publish Post At’endf.actions endend

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使用。

参考资料

https://github.com/justinfrench/formtastic

不论你在什么时候开始,重要的是开始之後就不要停止

ActiveAdmin 自定义表单

相关文章:

你感兴趣的文章:

标签云: