simple_form 3.0.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +149 -25
- data/MIT-LICENSE +1 -1
- data/README.md +441 -107
- data/lib/generators/simple_form/install_generator.rb +4 -3
- data/lib/generators/simple_form/templates/README +4 -5
- data/lib/generators/simple_form/templates/_form.html.erb +2 -0
- data/lib/generators/simple_form/templates/_form.html.haml +2 -0
- data/lib/generators/simple_form/templates/_form.html.slim +1 -0
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +48 -11
- data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +417 -23
- data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +101 -5
- data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -2
- data/lib/simple_form/action_view_extensions/builder.rb +2 -0
- data/lib/simple_form/action_view_extensions/form_helper.rb +10 -3
- data/lib/simple_form/components/errors.rb +38 -3
- data/lib/simple_form/components/hints.rb +3 -2
- data/lib/simple_form/components/html5.rb +16 -5
- data/lib/simple_form/components/label_input.rb +21 -2
- data/lib/simple_form/components/labels.rb +22 -11
- data/lib/simple_form/components/maxlength.rb +9 -5
- data/lib/simple_form/components/min_max.rb +2 -1
- data/lib/simple_form/components/minlength.rb +38 -0
- data/lib/simple_form/components/pattern.rb +2 -1
- data/lib/simple_form/components/placeholders.rb +4 -3
- data/lib/simple_form/components/readonly.rb +2 -1
- data/lib/simple_form/components.rb +2 -0
- data/lib/simple_form/error_notification.rb +1 -0
- data/lib/simple_form/form_builder.rb +193 -80
- data/lib/simple_form/helpers/autofocus.rb +1 -0
- data/lib/simple_form/helpers/disabled.rb +1 -0
- data/lib/simple_form/helpers/readonly.rb +1 -0
- data/lib/simple_form/helpers/required.rb +1 -0
- data/lib/simple_form/helpers/validators.rb +2 -1
- data/lib/simple_form/helpers.rb +6 -5
- data/lib/simple_form/i18n_cache.rb +1 -0
- data/lib/simple_form/inputs/base.rb +57 -14
- data/lib/simple_form/inputs/block_input.rb +2 -1
- data/lib/simple_form/inputs/boolean_input.rb +39 -16
- data/lib/simple_form/inputs/collection_check_boxes_input.rb +2 -1
- data/lib/simple_form/inputs/collection_input.rb +37 -14
- data/lib/simple_form/inputs/collection_radio_buttons_input.rb +9 -13
- data/lib/simple_form/inputs/collection_select_input.rb +5 -2
- data/lib/simple_form/inputs/date_time_input.rb +24 -9
- data/lib/simple_form/inputs/file_input.rb +5 -2
- data/lib/simple_form/inputs/grouped_collection_select_input.rb +16 -3
- data/lib/simple_form/inputs/hidden_input.rb +5 -2
- data/lib/simple_form/inputs/numeric_input.rb +6 -4
- data/lib/simple_form/inputs/password_input.rb +6 -3
- data/lib/simple_form/inputs/priority_input.rb +5 -2
- data/lib/simple_form/inputs/range_input.rb +2 -1
- data/lib/simple_form/inputs/string_input.rb +6 -3
- data/lib/simple_form/inputs/text_input.rb +6 -3
- data/lib/simple_form/inputs.rb +1 -0
- data/lib/simple_form/map_type.rb +1 -0
- data/lib/simple_form/railtie.rb +8 -0
- data/lib/simple_form/tags.rb +7 -0
- data/lib/simple_form/version.rb +2 -1
- data/lib/simple_form/wrappers/builder.rb +7 -6
- data/lib/simple_form/wrappers/leaf.rb +29 -0
- data/lib/simple_form/wrappers/many.rb +7 -6
- data/lib/simple_form/wrappers/root.rb +3 -1
- data/lib/simple_form/wrappers/single.rb +7 -4
- data/lib/simple_form/wrappers.rb +2 -0
- data/lib/simple_form.rb +117 -18
- data/test/action_view_extensions/builder_test.rb +42 -41
- data/test/action_view_extensions/form_helper_test.rb +36 -16
- data/test/components/custom_components_test.rb +62 -0
- data/test/components/label_test.rb +70 -41
- data/test/form_builder/association_test.rb +79 -37
- data/test/form_builder/button_test.rb +11 -10
- data/test/form_builder/error_notification_test.rb +2 -1
- data/test/form_builder/error_test.rb +173 -24
- data/test/form_builder/general_test.rb +171 -73
- data/test/form_builder/hint_test.rb +24 -18
- data/test/form_builder/input_field_test.rb +132 -56
- data/test/form_builder/label_test.rb +68 -13
- data/test/form_builder/wrapper_test.rb +190 -22
- data/test/generators/simple_form_generator_test.rb +8 -7
- data/test/inputs/boolean_input_test.rb +89 -6
- data/test/inputs/collection_check_boxes_input_test.rb +109 -25
- data/test/inputs/collection_radio_buttons_input_test.rb +168 -54
- data/test/inputs/collection_select_input_test.rb +183 -77
- data/test/inputs/datetime_input_test.rb +131 -50
- data/test/inputs/disabled_test.rb +16 -15
- data/test/inputs/discovery_test.rb +58 -6
- data/test/inputs/file_input_test.rb +3 -2
- data/test/inputs/general_test.rb +23 -22
- data/test/inputs/grouped_collection_select_input_test.rb +54 -17
- data/test/inputs/hidden_input_test.rb +5 -4
- data/test/inputs/numeric_input_test.rb +48 -44
- data/test/inputs/priority_input_test.rb +23 -14
- data/test/inputs/readonly_test.rb +20 -19
- data/test/inputs/required_test.rb +58 -13
- data/test/inputs/string_input_test.rb +62 -33
- data/test/inputs/text_input_test.rb +20 -7
- data/test/simple_form_test.rb +9 -0
- data/test/support/discovery_inputs.rb +33 -2
- data/test/support/misc_helpers.rb +113 -5
- data/test/support/mock_controller.rb +7 -1
- data/test/support/models.rb +137 -37
- data/test/test_helper.rb +15 -1
- metadata +25 -33
data/README.md
CHANGED
|
@@ -2,10 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
By [Plataformatec](http://plataformatec.com.br/).
|
|
4
4
|
|
|
5
|
-
[](http://badge.fury.io/rb/simple_form)
|
|
6
|
-
[](http://travis-ci.org/plataformatec/simple_form)
|
|
7
|
-
[](https://codeclimate.com/github/plataformatec/simple_form)
|
|
8
|
-
|
|
9
5
|
Rails forms made easy.
|
|
10
6
|
|
|
11
7
|
**Simple Form** aims to be as flexible as possible while helping you with powerful components to create
|
|
@@ -14,7 +10,7 @@ you find the better design for your eyes. Most of the DSL was inherited from For
|
|
|
14
10
|
which we are thankful for and should make you feel right at home.
|
|
15
11
|
|
|
16
12
|
INFO: This README is [also available in a friendly navigable format](http://simple-form.plataformatec.com.br/)
|
|
17
|
-
and refers to **Simple Form** 3.
|
|
13
|
+
and refers to **Simple Form** 3.1. For older releases, check the related branch for your version.
|
|
18
14
|
|
|
19
15
|
## Installation
|
|
20
16
|
|
|
@@ -36,34 +32,25 @@ Run the generator:
|
|
|
36
32
|
rails generate simple_form:install
|
|
37
33
|
```
|
|
38
34
|
|
|
39
|
-
|
|
40
|
-
[country_select gem](https://rubygems.org/gems/country_select), add it to your Gemfile:
|
|
41
|
-
|
|
42
|
-
```ruby
|
|
43
|
-
gem 'country_select'
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
### Twitter Bootstrap
|
|
35
|
+
### Bootstrap
|
|
47
36
|
|
|
48
|
-
**Simple Form** can be easily integrated to the [
|
|
37
|
+
**Simple Form** can be easily integrated to the [Bootstrap](http://getbootstrap.com/).
|
|
49
38
|
To do that you have to use the `bootstrap` option in the install generator, like this:
|
|
50
39
|
|
|
51
40
|
```console
|
|
52
41
|
rails generate simple_form:install --bootstrap
|
|
53
42
|
```
|
|
54
43
|
|
|
55
|
-
You have to be sure that you added a copy of the [
|
|
44
|
+
You have to be sure that you added a copy of the [Bootstrap](http://getbootstrap.com/)
|
|
56
45
|
assets on your application.
|
|
57
46
|
|
|
58
47
|
For more information see the generator output, our
|
|
59
48
|
[example application code](https://github.com/rafaelfranca/simple_form-bootstrap) and
|
|
60
49
|
[the live example app](http://simple-form-bootstrap.plataformatec.com.br/).
|
|
61
50
|
|
|
62
|
-
|
|
51
|
+
### Zurb Foundation 5
|
|
63
52
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
To generate wrappers that are compatible with [Zurb Foundation 3](http://foundation.zurb.com/), pass
|
|
53
|
+
To generate wrappers that are compatible with [Zurb Foundation 5](http://foundation.zurb.com/), pass
|
|
67
54
|
the `foundation` option to the generator, like this:
|
|
68
55
|
|
|
69
56
|
```console
|
|
@@ -74,14 +61,30 @@ Please note that the Foundation wrapper does not support the `:hint` option by d
|
|
|
74
61
|
enable hints, please uncomment the appropriate line in `config/initializers/simple_form_foundation.rb`.
|
|
75
62
|
You will need to provide your own CSS styles for hints.
|
|
76
63
|
|
|
77
|
-
Please see the [instructions on how to install Foundation in a Rails app](http://foundation.zurb.com/
|
|
64
|
+
Please see the [instructions on how to install Foundation in a Rails app](http://foundation.zurb.com/docs/applications.html).
|
|
65
|
+
|
|
66
|
+
### Country Select
|
|
67
|
+
|
|
68
|
+
If you want to use the country select, you will need the
|
|
69
|
+
[country_select gem](https://rubygems.org/gems/country_select), add it to your Gemfile:
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
gem 'country_select'
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
If you don't want to use the gem you can easily override this behaviour by mapping the
|
|
76
|
+
country inputs to something else, with a line like this in your `simple_form.rb` initializer:
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
config.input_mappings = { /country/ => :string }
|
|
80
|
+
```
|
|
78
81
|
|
|
79
82
|
## Usage
|
|
80
83
|
|
|
81
84
|
**Simple Form** was designed to be customized as you need to. Basically it's a stack of components that
|
|
82
85
|
are invoked to create a complete html input for you, which by default contains label, hints, errors
|
|
83
86
|
and the input itself. It does not aim to create a lot of different logic from the default Rails
|
|
84
|
-
form helpers, as they do a great
|
|
87
|
+
form helpers, as they do a great job by themselves. Instead, **Simple Form** acts as a DSL and just
|
|
85
88
|
maps your input type (retrieved from the column definition in the database) to a specific helper method.
|
|
86
89
|
|
|
87
90
|
To start using **Simple Form** you just have to use the helper it provides:
|
|
@@ -97,12 +100,12 @@ To start using **Simple Form** you just have to use the helper it provides:
|
|
|
97
100
|
This will generate an entire form with labels for user name and password as well, and render errors
|
|
98
101
|
by default when you render the form with invalid data (after submitting for example).
|
|
99
102
|
|
|
100
|
-
You can overwrite the default label by passing it to the input method. You can also add a hint
|
|
101
|
-
even a placeholder. For boolean inputs, you can add an inline label as well:
|
|
103
|
+
You can overwrite the default label by passing it to the input method. You can also add a hint,
|
|
104
|
+
an error, or even a placeholder. For boolean inputs, you can add an inline label as well:
|
|
102
105
|
|
|
103
106
|
```erb
|
|
104
107
|
<%= simple_form_for @user do |f| %>
|
|
105
|
-
<%= f.input :username, label: 'Your username please' %>
|
|
108
|
+
<%= f.input :username, label: 'Your username please', error: 'Username is mandatory, please specify one' %>
|
|
106
109
|
<%= f.input :password, hint: 'No special characters.' %>
|
|
107
110
|
<%= f.input :email, placeholder: 'user@domain.com' %>
|
|
108
111
|
<%= f.input :remember_me, inline_label: 'Yes, remember me' %>
|
|
@@ -110,7 +113,7 @@ even a placeholder. For boolean inputs, you can add an inline label as well:
|
|
|
110
113
|
<% end %>
|
|
111
114
|
```
|
|
112
115
|
|
|
113
|
-
In some cases you may want to disable labels, hints or
|
|
116
|
+
In some cases you may want to disable labels, hints or errors. Or you may want to configure the html
|
|
114
117
|
of any of them:
|
|
115
118
|
|
|
116
119
|
```erb
|
|
@@ -161,8 +164,9 @@ any html attribute to that wrapper as well using the `:wrapper_html` option, lik
|
|
|
161
164
|
|
|
162
165
|
Required fields are marked with an * prepended to their labels.
|
|
163
166
|
|
|
164
|
-
By default all inputs are required. When the form object
|
|
165
|
-
|
|
167
|
+
By default all inputs are required. When the form object includes `ActiveModel::Validations`
|
|
168
|
+
(which, for example, happens with Active Record models), fields are required only when there is `presence` validation.
|
|
169
|
+
Otherwise, **Simple Form** will mark fields as optional. For performance reasons, this
|
|
166
170
|
detection is skipped on validations that make use of conditional options, such as `:if` and `:unless`.
|
|
167
171
|
|
|
168
172
|
And of course, the `required` property of any input can be overwritten as needed:
|
|
@@ -176,6 +180,13 @@ And of course, the `required` property of any input can be overwritten as needed
|
|
|
176
180
|
<% end %>
|
|
177
181
|
```
|
|
178
182
|
|
|
183
|
+
By default, **Simple Form** will look at the column type in the database and use an
|
|
184
|
+
appropriate input for the column. For example, a column created with type
|
|
185
|
+
`:text` in the database will use a `textarea` input by default. See the section
|
|
186
|
+
[Available input types and defaults for each column
|
|
187
|
+
type](https://github.com/plataformatec/simple_form#available-input-types-and-defaults-for-each-column-type)
|
|
188
|
+
for a complete list of defaults.
|
|
189
|
+
|
|
179
190
|
**Simple Form** also lets you overwrite the default input type it creates:
|
|
180
191
|
|
|
181
192
|
```erb
|
|
@@ -189,11 +200,11 @@ And of course, the `required` property of any input can be overwritten as needed
|
|
|
189
200
|
```
|
|
190
201
|
|
|
191
202
|
So instead of a checkbox for the *accepts* attribute, you'll have a pair of radio buttons with yes/no
|
|
192
|
-
labels and a
|
|
203
|
+
labels and a textarea instead of a text field for the description. You can also render boolean
|
|
193
204
|
attributes using `as: :select` to show a dropdown.
|
|
194
205
|
|
|
195
206
|
It is also possible to give the `:disabled` option to **Simple Form**, and it'll automatically mark
|
|
196
|
-
the wrapper as disabled with a
|
|
207
|
+
the wrapper as disabled with a CSS class, so you can style labels, hints and other components inside
|
|
197
208
|
the wrapper as well:
|
|
198
209
|
|
|
199
210
|
```erb
|
|
@@ -203,13 +214,14 @@ the wrapper as well:
|
|
|
203
214
|
<% end %>
|
|
204
215
|
```
|
|
205
216
|
|
|
206
|
-
**Simple Form**
|
|
217
|
+
**Simple Form** inputs accept the same options as their corresponding input type helper in Rails:
|
|
207
218
|
|
|
208
219
|
```erb
|
|
209
220
|
<%= simple_form_for @user do |f| %>
|
|
210
221
|
<%= f.input :date_of_birth, as: :date, start_year: Date.today.year - 90,
|
|
211
222
|
end_year: Date.today.year - 12, discard_day: true,
|
|
212
223
|
order: [:month, :year] %>
|
|
224
|
+
<%= f.input :accepts, as: :boolean, checked_value: true, unchecked_value: false %>
|
|
213
225
|
<%= f.button :submit %>
|
|
214
226
|
<% end %>
|
|
215
227
|
```
|
|
@@ -241,9 +253,41 @@ Example:
|
|
|
241
253
|
```ruby
|
|
242
254
|
simple_form_for @user do |f|
|
|
243
255
|
f.input_field :name
|
|
256
|
+
f.input_field :remember_me, as: :boolean
|
|
244
257
|
end
|
|
245
258
|
```
|
|
246
259
|
|
|
260
|
+
```html
|
|
261
|
+
<form>
|
|
262
|
+
...
|
|
263
|
+
<input class="string required" id="user_name" maxlength="255" name="user[name]" size="255" type="text">
|
|
264
|
+
<input name="user[remember_me]" type="hidden" value="0">
|
|
265
|
+
<label class="checkbox">
|
|
266
|
+
<input class="boolean optional" id="user_published" name="user[remember_me]" type="checkbox" value="1">
|
|
267
|
+
</label>
|
|
268
|
+
</form>
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
For check boxes and radio buttons you can remove the label changing `boolean_style` from default value `:nested` to `:inline`.
|
|
272
|
+
|
|
273
|
+
Example:
|
|
274
|
+
|
|
275
|
+
```ruby
|
|
276
|
+
simple_form_for @user do |f|
|
|
277
|
+
f.input_field :name
|
|
278
|
+
f.input_field :remember_me, as: :boolean, boolean_style: :inline
|
|
279
|
+
end
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
```html
|
|
283
|
+
<form>
|
|
284
|
+
...
|
|
285
|
+
<input class="string required" id="user_name" maxlength="255" name="user[name]" size="255" type="text">
|
|
286
|
+
<input name="user[remember_me]" type="hidden" value="0">
|
|
287
|
+
<input class="boolean optional" id="user_remember_me" name="user[remember_me]" type="checkbox" value="1">
|
|
288
|
+
</form>
|
|
289
|
+
```
|
|
290
|
+
|
|
247
291
|
Produces:
|
|
248
292
|
|
|
249
293
|
```html
|
|
@@ -269,23 +313,37 @@ overriding the `:collection` option:
|
|
|
269
313
|
Collections can be arrays or ranges, and when a `:collection` is given the `:select` input will be
|
|
270
314
|
rendered by default, so we don't need to pass the `as: :select` option. Other types of collection
|
|
271
315
|
are `:radio_buttons` and `:check_boxes`. Those are added by **Simple Form** to Rails set of form
|
|
272
|
-
helpers (read Extra Helpers
|
|
316
|
+
helpers (read Extra Helpers section below for more information).
|
|
273
317
|
|
|
274
318
|
Collection inputs accept two other options beside collections:
|
|
275
319
|
|
|
276
|
-
*
|
|
320
|
+
* *label_method* => the label method to be applied to the collection to retrieve the label (use this
|
|
277
321
|
instead of the `text_method` option in `collection_select`)
|
|
278
322
|
|
|
279
|
-
*
|
|
323
|
+
* *value_method* => the value method to be applied to the collection to retrieve the value
|
|
280
324
|
|
|
281
325
|
Those methods are useful to manipulate the given collection. Both of these options also accept
|
|
282
326
|
lambda/procs in case you want to calculate the value or label in a special way eg. custom
|
|
283
|
-
translation.
|
|
284
|
-
|
|
327
|
+
translation. You can also define a `to_label` method on your model as **Simple Form** will search for
|
|
328
|
+
and use `:to_label` as a `:label_method` first if it is found.
|
|
329
|
+
|
|
330
|
+
By default, **Simple Form** will use the first item from an array as the label and the second one as the value.
|
|
331
|
+
If you want to change this behavior you must make it explicit, like this:
|
|
332
|
+
|
|
333
|
+
```erb
|
|
334
|
+
<%= simple_form_for @user do |f| %>
|
|
335
|
+
<%= f.input :gender, as: :radio_buttons, collection: [['0', 'female'], ['1', 'male']], label_method: :second, value_method: :first %>
|
|
336
|
+
<% end %>
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
All other options given are sent straight to the underlying helper. For example, you can give prompt as:
|
|
285
340
|
|
|
286
341
|
```ruby
|
|
287
|
-
f.input :age, collection: 18..60, prompt: "Select your age"
|
|
342
|
+
f.input :age, collection: 18..60, prompt: "Select your age", selected: 21
|
|
288
343
|
```
|
|
344
|
+
Extra options are passed into helper [`collection_select`](http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select).
|
|
345
|
+
|
|
346
|
+
You may also find it useful to explicitly pass a value to the optional `:selected`, especially if passing a collection of nested objects.
|
|
289
347
|
|
|
290
348
|
It is also possible to create grouped collection selects, that will use the html *optgroup* tags, like this:
|
|
291
349
|
|
|
@@ -296,24 +354,24 @@ f.input :country_id, collection: @continents, as: :grouped_select, group_method:
|
|
|
296
354
|
Grouped collection inputs accept the same `:label_method` and `:value_method` options, which will be
|
|
297
355
|
used to retrieve label/value attributes for the `option` tags. Besides that, you can give:
|
|
298
356
|
|
|
299
|
-
*
|
|
357
|
+
* *group_method* => the method to be called on the given collection to generate the options for
|
|
300
358
|
each group (required)
|
|
301
359
|
|
|
302
|
-
*
|
|
360
|
+
* *group_label_method* => the label method to be applied on the given collection to retrieve the label
|
|
303
361
|
for the _optgroup_ (**Simple Form** will attempt to guess the best one the same way it does with
|
|
304
362
|
`:label_method`)
|
|
305
363
|
|
|
306
364
|
### Priority
|
|
307
365
|
|
|
308
366
|
**Simple Form** also supports `:time_zone` and `:country`. When using such helpers, you can give
|
|
309
|
-
`:priority` as option to select which time zones and/or countries should be given higher priority:
|
|
367
|
+
`:priority` as an option to select which time zones and/or countries should be given higher priority:
|
|
310
368
|
|
|
311
369
|
```ruby
|
|
312
370
|
f.input :residence_country, priority: [ "Brazil" ]
|
|
313
371
|
f.input :time_zone, priority: /US/
|
|
314
372
|
```
|
|
315
373
|
|
|
316
|
-
Those values can also be configured with a default value to be used site
|
|
374
|
+
Those values can also be configured with a default value to be used on the site through the
|
|
317
375
|
`SimpleForm.country_priority` and `SimpleForm.time_zone_priority` helpers.
|
|
318
376
|
|
|
319
377
|
Note: While using `country_select` if you want to restrict to only a subset of countries for a specific
|
|
@@ -325,8 +383,8 @@ f.input :shipping_country, priority: [ "Brazil" ], collection: [ "Australia", "B
|
|
|
325
383
|
|
|
326
384
|
### Associations
|
|
327
385
|
|
|
328
|
-
To deal with associations, **Simple Form** can generate select inputs, a series of radios buttons or
|
|
329
|
-
Lets see how it works: imagine you have a user model that belongs to a company and has_and_belongs_to_many
|
|
386
|
+
To deal with associations, **Simple Form** can generate select inputs, a series of radios buttons or checkboxes.
|
|
387
|
+
Lets see how it works: imagine you have a user model that belongs to a company and `has_and_belongs_to_many`
|
|
330
388
|
roles. The structure would be something like:
|
|
331
389
|
|
|
332
390
|
```ruby
|
|
@@ -357,7 +415,7 @@ Now we have the user form:
|
|
|
357
415
|
|
|
358
416
|
Simple enough, right? This is going to render a `:select` input for choosing the `:company`, and another
|
|
359
417
|
`:select` input with `:multiple` option for the `:roles`. You can, of course, change it to use radio
|
|
360
|
-
buttons and
|
|
418
|
+
buttons and checkboxes as well:
|
|
361
419
|
|
|
362
420
|
```ruby
|
|
363
421
|
f.association :company, as: :radio_buttons
|
|
@@ -369,7 +427,7 @@ The association helper just invokes `input` under the hood, so all options avail
|
|
|
369
427
|
the collection by hand, all together with the prompt:
|
|
370
428
|
|
|
371
429
|
```ruby
|
|
372
|
-
f.association :company, collection: Company.active.
|
|
430
|
+
f.association :company, collection: Company.active.order(:name), prompt: "Choose a Company"
|
|
373
431
|
```
|
|
374
432
|
|
|
375
433
|
In case you want to declare different labels and values:
|
|
@@ -394,6 +452,22 @@ All web forms need buttons, right? **Simple Form** wraps them in the DSL, acting
|
|
|
394
452
|
|
|
395
453
|
The above will simply call submit. You choose to use it or not, it's just a question of taste.
|
|
396
454
|
|
|
455
|
+
The button method also accepts optional parameters, that are delegated to the underlying submit call:
|
|
456
|
+
|
|
457
|
+
```erb
|
|
458
|
+
<%= f.button :submit, "Custom Button Text", class: "my-button" %>
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
To create a `<button>` element, use the following syntax:
|
|
462
|
+
|
|
463
|
+
```erb
|
|
464
|
+
<%= f.button :button, "Custom Button Text" %>
|
|
465
|
+
|
|
466
|
+
<%= f.button :button do %>
|
|
467
|
+
Custom Button Text
|
|
468
|
+
<% end %>
|
|
469
|
+
```
|
|
470
|
+
|
|
397
471
|
### Wrapping Rails Form Helpers
|
|
398
472
|
|
|
399
473
|
Say you wanted to use a rails form helper but still wrap it in **Simple Form** goodness? You can, by
|
|
@@ -446,7 +520,7 @@ end
|
|
|
446
520
|
|
|
447
521
|
#### Collection Check Boxes
|
|
448
522
|
|
|
449
|
-
Creates a collection of
|
|
523
|
+
Creates a collection of checkboxes with labels associated (same API as `collection_select`):
|
|
450
524
|
|
|
451
525
|
```ruby
|
|
452
526
|
form_for @user do |f|
|
|
@@ -471,36 +545,41 @@ form_for @user do |f|
|
|
|
471
545
|
end
|
|
472
546
|
```
|
|
473
547
|
|
|
474
|
-
##
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
548
|
+
## Available input types and defaults for each column type
|
|
549
|
+
|
|
550
|
+
The following table shows the html element you will get for each attribute
|
|
551
|
+
according to its database definition. These defaults can be changed by
|
|
552
|
+
specifying the helper method in the column `Mapping` as the `as:` option.
|
|
553
|
+
|
|
554
|
+
Mapping | Generated HTML Element | Database Column Type
|
|
555
|
+
--------------- |--------------------------------------|---------------------
|
|
556
|
+
`boolean` | `input[type=checkbox]` | `boolean`
|
|
557
|
+
`string` | `input[type=text]` | `string`
|
|
558
|
+
`citext` | `input[type=text]` | `citext`
|
|
559
|
+
`email` | `input[type=email]` | `string` with `name =~ /email/`
|
|
560
|
+
`url` | `input[type=url]` | `string` with `name =~ /url/`
|
|
561
|
+
`tel` | `input[type=tel]` | `string` with `name =~ /phone/`
|
|
562
|
+
`password` | `input[type=password]` | `string` with `name =~ /password/`
|
|
563
|
+
`search` | `input[type=search]` | -
|
|
564
|
+
`uuid` | `input[type=text]` | `uuid`
|
|
565
|
+
`text` | `textarea` | `text`
|
|
566
|
+
`hstore` | `textarea` | `hstore`
|
|
567
|
+
`json` | `textarea` | `json`
|
|
568
|
+
`jsonb` | `textarea` | `jsonb`
|
|
569
|
+
`file` | `input[type=file]` | `string` responding to file methods
|
|
570
|
+
`hidden` | `input[type=hidden]` | -
|
|
571
|
+
`integer` | `input[type=number]` | `integer`
|
|
572
|
+
`float` | `input[type=number]` | `float`
|
|
573
|
+
`decimal` | `input[type=number]` | `decimal`
|
|
574
|
+
`range` | `input[type=range]` | -
|
|
575
|
+
`datetime` | `datetime select` | `datetime/timestamp`
|
|
576
|
+
`date` | `date select` | `date`
|
|
577
|
+
`time` | `time select` | `time`
|
|
578
|
+
`select` | `select` | `belongs_to`/`has_many`/`has_and_belongs_to_many` associations
|
|
579
|
+
`radio_buttons` | collection of `input[type=radio]` | `belongs_to` associations
|
|
580
|
+
`check_boxes` | collection of `input[type=checkbox]` | `has_many`/`has_and_belongs_to_many` associations
|
|
581
|
+
`country` | `select` (countries as options) | `string` with `name =~ /country/`
|
|
582
|
+
`time_zone` | `select` (timezones as options) | `string` with `name =~ /time_zone/`
|
|
504
583
|
|
|
505
584
|
## Custom inputs
|
|
506
585
|
|
|
@@ -510,8 +589,10 @@ that extends the string one, you just need to add this file:
|
|
|
510
589
|
```ruby
|
|
511
590
|
# app/inputs/currency_input.rb
|
|
512
591
|
class CurrencyInput < SimpleForm::Inputs::Base
|
|
513
|
-
def input
|
|
514
|
-
|
|
592
|
+
def input(wrapper_options)
|
|
593
|
+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
|
594
|
+
|
|
595
|
+
"$ #{@builder.text_field(attribute_name, merged_input_options)}".html_safe
|
|
515
596
|
end
|
|
516
597
|
end
|
|
517
598
|
```
|
|
@@ -521,6 +602,7 @@ And use it in your views:
|
|
|
521
602
|
```ruby
|
|
522
603
|
f.input :money, as: :currency
|
|
523
604
|
```
|
|
605
|
+
Note, you may have to create the `app/inputs/` directory and restart your webserver.
|
|
524
606
|
|
|
525
607
|
You can also redefine existing **Simple Form** inputs by creating a new class with the same name. For
|
|
526
608
|
instance, if you want to wrap date/time/datetime in a div, you can do:
|
|
@@ -528,7 +610,7 @@ instance, if you want to wrap date/time/datetime in a div, you can do:
|
|
|
528
610
|
```ruby
|
|
529
611
|
# app/inputs/date_time_input.rb
|
|
530
612
|
class DateTimeInput < SimpleForm::Inputs::DateTimeInput
|
|
531
|
-
def input
|
|
613
|
+
def input(wrapper_options)
|
|
532
614
|
template.content_tag(:div, super)
|
|
533
615
|
end
|
|
534
616
|
end
|
|
@@ -545,6 +627,28 @@ class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
|
|
|
545
627
|
end
|
|
546
628
|
```
|
|
547
629
|
|
|
630
|
+
If needed, you can namespace your custom inputs in a module and tell **Simple Form** to look for
|
|
631
|
+
their definitions in this module. This can avoid conflicts with other form libraries (like Formtastic) that look up
|
|
632
|
+
the global context to find inputs definition too.
|
|
633
|
+
|
|
634
|
+
```ruby
|
|
635
|
+
# app/inputs/custom_inputs/numeric_input
|
|
636
|
+
module CustomInputs
|
|
637
|
+
class NumericInput < SimpleForm::Inputs::NumericInput
|
|
638
|
+
def input_html_classes
|
|
639
|
+
super.push('no-spinner')
|
|
640
|
+
end
|
|
641
|
+
end
|
|
642
|
+
end
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
And in the **SimpleForm** initializer :
|
|
646
|
+
|
|
647
|
+
```ruby
|
|
648
|
+
# config/simple_form.rb
|
|
649
|
+
config.custom_inputs_namespaces << "CustomInputs"
|
|
650
|
+
```
|
|
651
|
+
|
|
548
652
|
## Custom form builder
|
|
549
653
|
|
|
550
654
|
You can create a custom form builder that uses **Simple Form**.
|
|
@@ -563,15 +667,14 @@ Create a form builder class that inherits from `SimpleForm::FormBuilder`.
|
|
|
563
667
|
```ruby
|
|
564
668
|
class CustomFormBuilder < SimpleForm::FormBuilder
|
|
565
669
|
def input(attribute_name, options = {}, &block)
|
|
566
|
-
options
|
|
567
|
-
super
|
|
670
|
+
super(attribute_name, options.merge(label: false), &block)
|
|
568
671
|
end
|
|
569
672
|
end
|
|
570
673
|
```
|
|
571
674
|
|
|
572
675
|
## I18n
|
|
573
676
|
|
|
574
|
-
**Simple Form** uses all power of I18n API to lookup labels, hints and placeholders. To customize your
|
|
677
|
+
**Simple Form** uses all power of I18n API to lookup labels, hints, prompts and placeholders. To customize your
|
|
575
678
|
forms you can create a locale file like this:
|
|
576
679
|
|
|
577
680
|
```yaml
|
|
@@ -589,12 +692,18 @@ en:
|
|
|
589
692
|
user:
|
|
590
693
|
username: 'Your username'
|
|
591
694
|
password: '****'
|
|
695
|
+
include_blanks:
|
|
696
|
+
user:
|
|
697
|
+
age: 'Rather not say'
|
|
698
|
+
prompts:
|
|
699
|
+
user:
|
|
700
|
+
role: 'Select your role'
|
|
592
701
|
```
|
|
593
702
|
|
|
594
703
|
And your forms will use this information to render the components for you.
|
|
595
704
|
|
|
596
|
-
**Simple Form** also lets you be more specific, separating lookups through actions
|
|
597
|
-
|
|
705
|
+
**Simple Form** also lets you be more specific, separating lookups through actions.
|
|
706
|
+
Let's say you want a different label for new and edit actions, the locale file would
|
|
598
707
|
be something like:
|
|
599
708
|
|
|
600
709
|
```yaml
|
|
@@ -633,20 +742,26 @@ en:
|
|
|
633
742
|
```
|
|
634
743
|
|
|
635
744
|
**Simple Form** will always look for a default attribute translation under the "defaults" key if no
|
|
636
|
-
specific is found inside the model key.
|
|
637
|
-
the new syntax, just move "labels.#{attribute}" to "labels.defaults.#{attribute}".
|
|
745
|
+
specific is found inside the model key.
|
|
638
746
|
|
|
639
|
-
In addition, **Simple Form** will fallback to default human_attribute_name from Rails when no other
|
|
747
|
+
In addition, **Simple Form** will fallback to default `human_attribute_name` from Rails when no other
|
|
640
748
|
translation is found for labels. Finally, you can also overwrite any label, hint or placeholder
|
|
641
749
|
inside your view, just by passing the option manually. This way the I18n lookup will be skipped.
|
|
642
750
|
|
|
751
|
+
For `:prompt` and `:include_blank` the I18n lookup is optional and to enable it is necessary to pass
|
|
752
|
+
`:translate` as value.
|
|
753
|
+
|
|
754
|
+
```ruby
|
|
755
|
+
f.input :role, prompt: :translate
|
|
756
|
+
```
|
|
757
|
+
|
|
643
758
|
**Simple Form** also has support for translating options in collection helpers. For instance, given a
|
|
644
|
-
User with a `:
|
|
645
|
-
that would post either `
|
|
759
|
+
User with a `:role` attribute, you might want to create a select box showing translated labels
|
|
760
|
+
that would post either `:admin` or `:editor` as value. With **Simple Form** you could create an input
|
|
646
761
|
like this:
|
|
647
762
|
|
|
648
763
|
```ruby
|
|
649
|
-
f.input :
|
|
764
|
+
f.input :role, collection: [:admin, :editor]
|
|
650
765
|
```
|
|
651
766
|
|
|
652
767
|
And **Simple Form** will try a lookup like this in your locale file, to find the right labels to show:
|
|
@@ -656,9 +771,9 @@ en:
|
|
|
656
771
|
simple_form:
|
|
657
772
|
options:
|
|
658
773
|
user:
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
774
|
+
role:
|
|
775
|
+
admin: 'Administrator'
|
|
776
|
+
editor: 'Editor'
|
|
662
777
|
```
|
|
663
778
|
|
|
664
779
|
You can also use the `defaults` key as you would do with labels, hints and placeholders. It is
|
|
@@ -711,6 +826,22 @@ object itself. Thus, similarly, if a form for an `Admin::User` object is defined
|
|
|
711
826
|
`simple_form_for @admin_user, as: :some_user`, **Simple Form** will look for translations
|
|
712
827
|
under `some_user` instead of `admin_user`.
|
|
713
828
|
|
|
829
|
+
When translating `simple_fields_for` attributes be sure to use the same name you pass to it, e.g. `simple_fields_for :posts` should be placed under `posts` not `post`:
|
|
830
|
+
|
|
831
|
+
```yaml
|
|
832
|
+
en:
|
|
833
|
+
simple_form:
|
|
834
|
+
labels:
|
|
835
|
+
posts:
|
|
836
|
+
title: 'Post title'
|
|
837
|
+
hints:
|
|
838
|
+
posts:
|
|
839
|
+
title: 'A good title'
|
|
840
|
+
placeholders:
|
|
841
|
+
posts:
|
|
842
|
+
title: 'Once upon a time...'
|
|
843
|
+
```
|
|
844
|
+
|
|
714
845
|
## Configuration
|
|
715
846
|
|
|
716
847
|
**Simple Form** has several configuration options. You can read and change them in the initializer
|
|
@@ -725,7 +856,8 @@ The syntax looks like this:
|
|
|
725
856
|
|
|
726
857
|
```ruby
|
|
727
858
|
config.wrappers tag: :div, class: :input,
|
|
728
|
-
error_class: :field_with_errors
|
|
859
|
+
error_class: :field_with_errors,
|
|
860
|
+
valid_class: :field_without_errors do |b|
|
|
729
861
|
|
|
730
862
|
# Form extensions
|
|
731
863
|
b.use :html5
|
|
@@ -770,13 +902,24 @@ end
|
|
|
770
902
|
|
|
771
903
|
this will wrap the hint and error components within a `div` tag using the class `'separator'`.
|
|
772
904
|
|
|
905
|
+
You can customize _Form components_ passing options to them:
|
|
906
|
+
|
|
907
|
+
```ruby
|
|
908
|
+
config.wrappers do |b|
|
|
909
|
+
b.use :label_input, class: 'label-input-class', error_class: 'is-invalid', valid_class: 'is-valid'
|
|
910
|
+
end
|
|
911
|
+
```
|
|
912
|
+
|
|
913
|
+
This you set the input and label class to `'label-input-class'` and will set the class `'is-invalid'`
|
|
914
|
+
when the input has errors and `'is-valid'` if the input is valid.
|
|
915
|
+
|
|
773
916
|
If you want to customize the custom _Form components_ on demand you can give it a name like this:
|
|
774
917
|
|
|
775
918
|
```ruby
|
|
776
919
|
config.wrappers do |b|
|
|
777
920
|
b.use :placeholder
|
|
778
921
|
b.use :label_input
|
|
779
|
-
b.wrapper :my_wrapper, tag: :div, class: 'separator' do |component|
|
|
922
|
+
b.wrapper :my_wrapper, tag: :div, class: 'separator', html: { id: 'my_wrapper_id' } do |component|
|
|
780
923
|
component.use :hint, wrap_with: { tag: :span, class: :hint }
|
|
781
924
|
component.use :error, wrap_with: { tag: :span, class: :error }
|
|
782
925
|
end
|
|
@@ -838,6 +981,101 @@ end
|
|
|
838
981
|
By setting it as `optional`, a hint will only be generated when `hint: true` is explicitly used.
|
|
839
982
|
The same for placeholder.
|
|
840
983
|
|
|
984
|
+
It is also possible to give the option `:unless_blank` to the wrapper if you want to render it only
|
|
985
|
+
when the content is present.
|
|
986
|
+
|
|
987
|
+
```ruby
|
|
988
|
+
b.wrapper tag: :span, class: 'hint', unless_blank: true do |component|
|
|
989
|
+
component.optional :hint
|
|
990
|
+
end
|
|
991
|
+
```
|
|
992
|
+
|
|
993
|
+
## Custom Components
|
|
994
|
+
|
|
995
|
+
When you use custom wrappers, you might also be looking for a way to add custom components to your
|
|
996
|
+
wrapper. The default components are:
|
|
997
|
+
|
|
998
|
+
```ruby
|
|
999
|
+
:label # The <label> tag alone
|
|
1000
|
+
:input # The <input> tag alone
|
|
1001
|
+
:label_input # The <label> and the <input> tags
|
|
1002
|
+
:hint # The hint for the input
|
|
1003
|
+
:error # The error for the input
|
|
1004
|
+
```
|
|
1005
|
+
|
|
1006
|
+
A custom component might be interesting for you if your views look something like this:
|
|
1007
|
+
|
|
1008
|
+
```erb
|
|
1009
|
+
<%= simple_form_for @blog do |f| %>
|
|
1010
|
+
<div class="row">
|
|
1011
|
+
<div class="span1 number">
|
|
1012
|
+
1
|
|
1013
|
+
</div>
|
|
1014
|
+
<div class="span8">
|
|
1015
|
+
<%= f.input :title %>
|
|
1016
|
+
</div>
|
|
1017
|
+
</div>
|
|
1018
|
+
<div class="row">
|
|
1019
|
+
<div class="span1 number">
|
|
1020
|
+
2
|
|
1021
|
+
</div>
|
|
1022
|
+
<div class="span8">
|
|
1023
|
+
<%= f.input :body, as: :text %>
|
|
1024
|
+
</div>
|
|
1025
|
+
</div>
|
|
1026
|
+
<% end %>
|
|
1027
|
+
```
|
|
1028
|
+
|
|
1029
|
+
A cleaner method to create your views would be:
|
|
1030
|
+
|
|
1031
|
+
```erb
|
|
1032
|
+
<%= simple_form_for @blog, wrapper: :with_numbers do |f| %>
|
|
1033
|
+
<%= f.input :title, number: 1 %>
|
|
1034
|
+
<%= f.input :body, as: :text, number: 2 %>
|
|
1035
|
+
<% end %>
|
|
1036
|
+
```
|
|
1037
|
+
|
|
1038
|
+
To use the number option on the input, first, tells to Simple Form the place where the components
|
|
1039
|
+
will be:
|
|
1040
|
+
|
|
1041
|
+
``` ruby
|
|
1042
|
+
# config/initializers/simple_form.rb
|
|
1043
|
+
Dir[Rails.root.join('lib/components/**/*.rb')].each { |f| require f }
|
|
1044
|
+
```
|
|
1045
|
+
|
|
1046
|
+
Create a new component within the path specified above:
|
|
1047
|
+
|
|
1048
|
+
```ruby
|
|
1049
|
+
# lib/components/numbers_component.rb
|
|
1050
|
+
module NumbersComponent
|
|
1051
|
+
# To avoid deprecation warning, you need to make the wrapper_options explicit
|
|
1052
|
+
# even when they won't be used.
|
|
1053
|
+
def number(wrapper_options = nil)
|
|
1054
|
+
@number ||= begin
|
|
1055
|
+
options[:number].to_s.html_safe if options[:number].present?
|
|
1056
|
+
end
|
|
1057
|
+
end
|
|
1058
|
+
end
|
|
1059
|
+
|
|
1060
|
+
SimpleForm.include_component(NumbersComponent)
|
|
1061
|
+
```
|
|
1062
|
+
|
|
1063
|
+
Finally, add a new wrapper to the config/initializers/simple_form.rb file:
|
|
1064
|
+
|
|
1065
|
+
```ruby
|
|
1066
|
+
config.wrappers :with_numbers, tag: 'div', class: 'row', error_class: 'error' do |b|
|
|
1067
|
+
b.use :html5
|
|
1068
|
+
b.use :number, wrap_with: { tag: 'div', class: 'span1 number'}
|
|
1069
|
+
b.wrapper tag: 'div', class: 'span8' do |ba|
|
|
1070
|
+
ba.use :placeholder
|
|
1071
|
+
ba.use :label
|
|
1072
|
+
ba.use :input
|
|
1073
|
+
ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
|
|
1074
|
+
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
|
|
1075
|
+
end
|
|
1076
|
+
end
|
|
1077
|
+
```
|
|
1078
|
+
|
|
841
1079
|
## HTML 5 Notice
|
|
842
1080
|
|
|
843
1081
|
By default, **Simple Form** will generate input field types and attributes that are supported in HTML5,
|
|
@@ -850,10 +1088,25 @@ required attribute to force a value into an input and will prevent form submissi
|
|
|
850
1088
|
Depending on the design of the application this may or may not be desired. In many cases it can
|
|
851
1089
|
break existing UI's.
|
|
852
1090
|
|
|
853
|
-
It is possible to disable all HTML 5 extensions in **Simple Form**
|
|
1091
|
+
It is possible to disable all HTML 5 extensions in **Simple Form** by removing the `html5`
|
|
1092
|
+
component from the wrapper used to render the inputs.
|
|
1093
|
+
|
|
1094
|
+
For example, change:
|
|
854
1095
|
|
|
855
1096
|
```ruby
|
|
856
|
-
|
|
1097
|
+
config.wrappers tag: :div do |b|
|
|
1098
|
+
b.use :html5
|
|
1099
|
+
|
|
1100
|
+
b.use :label_input
|
|
1101
|
+
end
|
|
1102
|
+
```
|
|
1103
|
+
|
|
1104
|
+
To:
|
|
1105
|
+
|
|
1106
|
+
```ruby
|
|
1107
|
+
config.wrappers tag: :div do |b|
|
|
1108
|
+
b.use :label_input
|
|
1109
|
+
end
|
|
857
1110
|
```
|
|
858
1111
|
|
|
859
1112
|
If you want to have all other HTML 5 features, such as the new field types, you can disable only
|
|
@@ -870,14 +1123,93 @@ help you to use some generic javascript validation.
|
|
|
870
1123
|
You can also add `novalidate` to a specific form by setting the option on the form itself:
|
|
871
1124
|
|
|
872
1125
|
```erb
|
|
873
|
-
<%= simple_form_for(resource, html: {novalidate: true}) do |form| %>
|
|
1126
|
+
<%= simple_form_for(resource, html: { novalidate: true }) do |form| %>
|
|
1127
|
+
```
|
|
1128
|
+
|
|
1129
|
+
Please notice that none of the configurations above will disable the `placeholder` component,
|
|
1130
|
+
which is an HTML 5 feature. We believe most of the newest browsers are handling this attribute
|
|
1131
|
+
just fine, and if they aren't, any plugin you use would take care of applying the placeholder.
|
|
1132
|
+
In any case, you can disable it if you really want to, by removing the placeholder component
|
|
1133
|
+
from the components list in the **Simple Form** configuration file.
|
|
1134
|
+
|
|
1135
|
+
HTML 5 date / time inputs are not generated by **Simple Form** by default, so using `date`,
|
|
1136
|
+
`time` or `datetime` will all generate select boxes using normal Rails helpers. We believe
|
|
1137
|
+
browsers are not totally ready for these yet, but you can easily opt-in on a per-input basis
|
|
1138
|
+
by passing the html5 option:
|
|
1139
|
+
|
|
1140
|
+
```erb
|
|
1141
|
+
<%= f.input :expires_at, as: :date, html5: true %>
|
|
1142
|
+
```
|
|
1143
|
+
|
|
1144
|
+
### Using non Active Record objects
|
|
1145
|
+
|
|
1146
|
+
There are few ways to build forms with objects that don't inherit from Active Record, as
|
|
1147
|
+
follow:
|
|
1148
|
+
|
|
1149
|
+
You can include the module `ActiveModel::Model`.
|
|
1150
|
+
|
|
1151
|
+
```ruby
|
|
1152
|
+
class User
|
|
1153
|
+
include ActiveModel::Model
|
|
1154
|
+
|
|
1155
|
+
attr_accessor :id, :name
|
|
1156
|
+
end
|
|
1157
|
+
```
|
|
1158
|
+
|
|
1159
|
+
If you are using Presenters or Decorators that inherit from `SimpleDelegator` you can delegate
|
|
1160
|
+
it to the model.
|
|
1161
|
+
|
|
1162
|
+
```ruby
|
|
1163
|
+
class UserPresenter < SimpleDelegator
|
|
1164
|
+
# Without that, Simple Form will consider the user model as the object.
|
|
1165
|
+
def to_model
|
|
1166
|
+
self
|
|
1167
|
+
end
|
|
1168
|
+
end
|
|
1169
|
+
```
|
|
1170
|
+
|
|
1171
|
+
You can define all methods required by the helpers.
|
|
1172
|
+
|
|
1173
|
+
```ruby
|
|
1174
|
+
class User
|
|
1175
|
+
extend ActiveModel::Naming
|
|
1176
|
+
|
|
1177
|
+
attr_accessor :id, :name
|
|
1178
|
+
|
|
1179
|
+
def to_model
|
|
1180
|
+
self
|
|
1181
|
+
end
|
|
1182
|
+
|
|
1183
|
+
def to_key
|
|
1184
|
+
id
|
|
1185
|
+
end
|
|
1186
|
+
|
|
1187
|
+
def persisted?
|
|
1188
|
+
false
|
|
1189
|
+
end
|
|
1190
|
+
end
|
|
874
1191
|
```
|
|
875
1192
|
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
1193
|
+
If your object doesn't implement those methods, you must make explicit it when you are
|
|
1194
|
+
building the form
|
|
1195
|
+
|
|
1196
|
+
```ruby
|
|
1197
|
+
class User
|
|
1198
|
+
attr_accessor :id, :name
|
|
1199
|
+
|
|
1200
|
+
# The only method required to use the f.submit helper.
|
|
1201
|
+
def persisted?
|
|
1202
|
+
false
|
|
1203
|
+
end
|
|
1204
|
+
end
|
|
1205
|
+
```
|
|
1206
|
+
|
|
1207
|
+
```erb
|
|
1208
|
+
<%= simple_form_for(@user, as: :user, method: :post, url: users_path) do |f| %>
|
|
1209
|
+
<%= f.input :name %>
|
|
1210
|
+
<%= f.submit 'New user' %>
|
|
1211
|
+
<% end %>
|
|
1212
|
+
```
|
|
881
1213
|
|
|
882
1214
|
## Information
|
|
883
1215
|
|
|
@@ -894,13 +1226,10 @@ You can view the **Simple Form** documentation in RDoc format here:
|
|
|
894
1226
|
|
|
895
1227
|
http://rubydoc.info/github/plataformatec/simple_form/master/frames
|
|
896
1228
|
|
|
897
|
-
If you need to use **Simple Form** with Rails 2.3, you can always run `gem server` from the command line
|
|
898
|
-
after you install the gem to access the old documentation.
|
|
899
|
-
|
|
900
1229
|
### Bug reports
|
|
901
1230
|
|
|
902
1231
|
If you discover any bugs, feel free to create an issue on GitHub. Please add as much information as
|
|
903
|
-
possible to help us fixing the
|
|
1232
|
+
possible to help us in fixing the potential bug. We also encourage you to help even more by forking and
|
|
904
1233
|
sending us a pull request.
|
|
905
1234
|
|
|
906
1235
|
https://github.com/plataformatec/simple_form/issues
|
|
@@ -912,9 +1241,14 @@ https://github.com/plataformatec/simple_form/issues
|
|
|
912
1241
|
* Rafael Mendonça França (https://github.com/rafaelfranca)
|
|
913
1242
|
* Vasiliy Ermolovich (https://github.com/nashby)
|
|
914
1243
|
|
|
1244
|
+
[](http://badge.fury.io/rb/simple_form)
|
|
1245
|
+
[](http://travis-ci.org/plataformatec/simple_form)
|
|
1246
|
+
[](https://codeclimate.com/github/plataformatec/simple_form)
|
|
1247
|
+
[](http://inch-ci.org/github/plataformatec/simple_form)
|
|
1248
|
+
|
|
915
1249
|
## License
|
|
916
1250
|
|
|
917
|
-
MIT License. Copyright 2009-
|
|
1251
|
+
MIT License. Copyright 2009-2018 Plataformatec. http://plataformatec.com.br
|
|
918
1252
|
|
|
919
1253
|
You are not granted rights or licenses to the trademarks of the Plataformatec, including without
|
|
920
1254
|
limitation the Simple Form name or logo.
|