Method: Hanami::Helpers::FormHelper#form_for

Defined in:
lib/hanami/helpers/form_helper.rb

#form_for(base_name, url, values: _form_for_values, params: _form_for_params, **attributes) {|f| ... } ⇒ String #form_for(url, values: _form_for_values, params: _form_for_params, **attributes) {|f| ... } ⇒ String

Yields a form builder for constructing an HTML form and returns the resulting form string.

See FormBuilder for the methods for building the form’s fields.

Examples:

Basic usage

<%= form_for("book", "/books", class: "form-horizontal") do |f| %>
  <div>
    <%= f.label "title" %>
    <%= f.text_field "title", class: "form-control" %>
  </div>

  <%= f.submit "Create" %>
<% end %>

=>
<form action="/books" method="POST" accept-charset="utf-8" class="form-horizontal">
  <input type="hidden" name="_csrf_token" value="920cd5bfaecc6e58368950e790f2f7b4e5561eeeab230aa1b7de1b1f40ea7d5d">
  <div>
    <label for="book-title">Title</label>
    <input type="text" name="book[title]" id="book-title" value="Test Driven Development">
  </div>

  <button type="submit">Create</button>
</form>

Without base name


<%= form_for("/books", class: "form-horizontal") do |f| %>
  <div>
    <%= f.label "books.title" %>
    <%= f.text_field "books.title", class: "form-control" %>
  </div>

  <%= f.submit "Create" %>
<% end %>

=>
<form action="/books" method="POST" accept-charset="utf-8" class="form-horizontal">
  <input type="hidden" name="_csrf_token" value="920cd5bfaecc6e58368950e790f2f7b4e5561eeeab230aa1b7de1b1f40ea7d5d">
  <div>
    <label for="book-title">Title</label>
    <input type="text" name="book[title]" id="book-title" value="Test Driven Development">
  </div>

  <button type="submit">Create</button>
</form>

Method override

<%= form_for("/books/123", method: :put) do |f|
  <%= f.text_field "book.title" %>
  <%= f.submit "Update" %>
<% end %>

=>
<form action="/books/123" accept-charset="utf-8" method="POST">
  <input type="hidden" name="_method" value="PUT">
  <input type="hidden" name="_csrf_token" value="920cd5bfaecc6e58368950e790f2f7b4e5561eeeab230aa1b7de1b1f40ea7d5d">
  <input type="text" name="book[title]" id="book-title" value="Test Driven Development">

  <button type="submit">Update</button>
</form>

Overriding values

<%= form_for("/songs", values: {song: {title: "Envision"}}) do |f| %>
  <%= f.text_field "song.title" %>
  <%= f.submit "Create" %>
<%= end %>

=>
<form action="/songs" accept-charset="utf-8" method="POST">
  <input type="hidden" name="_csrf_token" value="920cd5bfaecc6e58368950e790f2f7b4e5561eeeab230aa1b7de1b1f40ea7d5d">
  <input type="text" name="song[title]" id="song-title" value="Envision">

  <button type="submit">Create</button>
</form>

Overloads:

  • #form_for(base_name, url, values: _form_for_values, params: _form_for_params, **attributes) {|f| ... } ⇒ String

    Builds the form using the given base name for all fields.

    Parameters:

    • base_name (String)

      the base

    • url (String)

      the URL for submitting the form

    • values (Hash) (defaults to: _form_for_values)

      values to be used for populating form field values; optional, defaults to the template’s locals or to a part’s ‘=> self`

    • params (Hash) (defaults to: _form_for_params)

      request param values to be used for populating form field values; these are used in preference over the values; optional, defaults to the current request’s params

    • attributes (Hash)

      the HTML attributes for the form tag

    Yield Parameters:

  • #form_for(url, values: _form_for_values, params: _form_for_params, **attributes) {|f| ... } ⇒ String

    Parameters:

    • url (String)

      the URL for submitting the form

    • values (Hash) (defaults to: _form_for_values)

      values to be used for populating form field values; optional, defaults to the template’s locals or to a part’s ‘=> self`

    • params (Hash) (defaults to: _form_for_params)

      request param values to be used for populating form field values; these are used in preference over the values; optional, defaults to the current request’s params

    • attributes (Hash)

      the HTML attributes for the form tag

    Yield Parameters:

Returns:

  • (String)

    the form HTML

See Also:

Since:

  • 2.1.0



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/hanami/helpers/form_helper.rb', line 149

def form_for(base_name, url = nil, values: _form_for_values, params: _form_for_params, **attributes)
  url, base_name = base_name, nil if url.nil?

  values = Values.new(values: values, params: params, csrf_token: _form_csrf_token)

  builder = FormBuilder.new(
    base_name: base_name,
    values: values,
    inflector: _context.inflector,
    form_attributes: attributes
  )

  content = (block_given? ? yield(builder) : "").html_safe

  builder.call(content, action: url, **attributes)
end