gem 'simple_form'
bundle install
rails generate simple_form:install
rails generate simple_form:install --bootstrap
<%= simple_form_for @user do |f| %>
<%= f.input :username, label: 'Your username please' %>
<%= f.input :password, hint: 'No special characters.' %>
<%= f.input :email, placeholder: '[email protected]' %>
<%= f.input :remember_me, inline_label: 'Yes, remember me' %>
<%= f.simple_fields_for :posts do |post| %>
<%= post.input :name %>
<% end %>
<%= f.button :submit %>
<% end %>
= f.input :username,
label: 'Your username please' # 标签
label_html: { class: 'my_class' } #标签的html设置
hint: 'No special characters.' # 提示语,显示在下面
placeholder: '[email protected]' # 提示语
inline_label: 'Yes, remember me' # 内部标签
error_html: { id: 'password_error'} #错误提示样式
label: false # 不需要标签
input_html: { class: 'special', maxlength: 20, value: '1', readonly: true } # 输入内容:类,最大长度,默认值,只读
required: false # 根据model validate 会显示必填,可以去掉
wrapper_html: { class: 'username', id: 'password' } # 包含标签的使用
prompt: "Select your age"
as: :text,
:string(常用在时间上),
:radio_buttons
:check_boxes
:boolean, checked_value: true, unchecked_value: false, boolean_style: :inline(||:nested)
:select, collection: 18..100,
label_method: :lms_course_base_info(可在model里面设置),
value_method:
include_blank: false,
priority: [30]
关联关系
f.association :roles, as: :check_boxes
# 这里可用as: :select 来代替
Mapping | Generated HTML Element | Database Column Type
--------------- |:-------------------------------------|:--------------------
`boolean` | `input[type=checkbox]` | `boolean`
`string` | `input[type=text]` | `string`
`email` | `input[type=email]` | `string` with `name =~ /email/`
`url` | `input[type=url]` | `string` with `name =~ /url/`
`tel` | `input[type=tel]` | `string` with `name =~ /phone/`
`password` | `input[type=password]` | `string` with `name =~ /password/`
`search` | `input[type=search]` | -
`uuid` | `input[type=text]` | `uuid`
`text` | `textarea` | `text`
`file` | `input[type=file]` | `string` responding to file methods
`hidden` | `input[type=hidden]` | -
`integer` | `input[type=number]` | `integer`
`float` | `input[type=number]` | `float`
`decimal` | `input[type=number]` | `decimal`
`range` | `input[type=range]` | -
`datetime` | `datetime select` | `datetime/timestamp`
`date` | `date select` | `date`
`time` | `time select` | `time`
`select` | `select` | `belongs_to`/`has_many`/`has_and_belongs_to_many` associations
`radio_buttons` | collection of `input[type=radio]` | `belongs_to` associations
`check_boxes` | collection of `input[type=checkbox]` | `has_many`/`has_and_belongs_to_many` associations
`country` | `select` (countries as options) | `string` with `name =~ /country/`
`time_zone` | `select` (timezones as options) | `string` with `name =~ /time_zone/`