fields
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7 (0)
- 5.2.3 (-38)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
fields(scope = nil, model: nil, **options, &block)
public
Scopes input fields with either an explicit scope or model. Like form_with does with :scope or :model, except it doesn’t output the form tags.
# Using a scope prefixes the input field names: <%= fields :comment do |fields| %> <%= fields.text_field :body %> <% end %> # => <input type="text" name="comment[body]"> # Using a model infers the scope and assigns field values: <%= fields model: Comment.new(body: "full bodied") do |fields| %> <%= fields.text_field :body %> <% end %> # => <input type="text" name="comment[body]" value="full bodied"> # Using +fields+ with +form_with+: <%= form_with model: @post do |form| %> <%= form.text_field :title %> <%= form.fields :comment do |fields| %> <%= fields.text_field :body %> <% end %> <% end %>
Much like form_with a FormBuilder instance associated with the scope or model is yielded, so any generated field names are prefixed with either the passed scope or the scope inferred from the :model.
Mixing with other form helpers
While form_with uses a FormBuilder object it’s possible to mix and match the stand-alone FormHelper methods and methods from FormTagHelper:
<%= fields model: @comment do |fields| %> <%= fields.text_field :body %> <%= text_area :commenter, :biography %> <%= check_box_tag "comment[all_caps]", "1", @comment.commenter.hulk_mode? %> <% end %>
Same goes for the methods in FormOptionsHelper and DateHelper designed to work with an object as a base, like FormOptionsHelper#collection_select and DateHelper#datetime_select.