class FormWithActsLikeFormForTest

Constants

Routes

Public Instance Methods

_routes() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 271
def _routes
  Routes
end
form_with(*) click to toggle source
Calls superclass method
# File actionview/test/template/form_helper/form_with_test.rb, line 134
def form_with(*)
  @output_buffer = super
end
private_property() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 384
        def private_property
  "That would be great."
end
protected_property() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 388
          def protected_property
  "I believe you have my stapler."
end
test_deep_nested_fields() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 991
def test_deep_nested_fields
  @comment.save
  form_with(scope: :posts) do |f|
    f.fields("post[]", model: @post) do |f2|
      f2.text_field(:id)
      @post.comments.each do |comment|
        concat f2.fields("comment[]", model: comment) { |c|
          concat c.text_field(:name)
        }
      end
    end
  end

  expected = whole_form do
    "<input name='posts[post][0][comment][1][name]' type='text' value='comment #1' />"
  end

  assert_dom_equal expected, output_buffer
end
test_default_form_builder() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1953
def test_default_form_builder
  old_default_form_builder, ActionView::Base.default_form_builder =
    ActionView::Base.default_form_builder, LabelledFormBuilder

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<label for='title'>Title:</label> <input name='post[title]' type='text' value='Hello World' /><br/>" \
    "<label for='body'>Body:</label> <textarea name='post[body]'>\nBack to the hill and over it again!</textarea><br/>" \
    "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' value='1' /><br/>"
  end

  assert_dom_equal expected, output_buffer
ensure
  ActionView::Base.default_form_builder = old_default_form_builder
end
test_fields() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1752
def test_fields
  output_buffer = fields(:post, model: @post) do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected =
    "<input name='post[title]' type='text' value='Hello World' />" \
    "<textarea name='post[body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[secret]' type='hidden' value='0' />" \
    "<input name='post[secret]' checked='checked' type='checkbox' value='1' />"

  assert_dom_equal expected, output_buffer
end
test_fields_object_with_bracketed_name() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1848
def test_fields_object_with_bracketed_name
  output_buffer = fields("author[post]", model: @post) do |f|
    concat f.label(:title)
    concat f.text_field(:title)
  end

  assert_dom_equal "<label for=\"author_post_title\">Title</label>" \
  "<input name='author[post][title]' type='text' value='Hello World' />",
    output_buffer
end
test_fields_object_with_bracketed_name_and_index() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1859
def test_fields_object_with_bracketed_name_and_index
  output_buffer = fields("author[post]", model: @post, index: 1) do |f|
    concat f.label(:title)
    concat f.text_field(:title)
  end

  assert_dom_equal "<label for=\"author_post_1_title\">Title</label>" \
    "<input name='author[post][1][title]' type='text' value='Hello World' />",
    output_buffer
end
test_fields_returns_block_result() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2176
def test_fields_returns_block_result
  output = fields(model: Post.new) { |f| "fields" }
  assert_equal "fields", output
end
test_fields_with_attributes_not_on_model() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 942
def test_fields_with_attributes_not_on_model
  form_with(model: @post) do |f|
    concat f.fields(:comment) { |c|
      concat c.text_field :dont_exist_on_model
    }
  end

  expected = whole_form("/posts/123", method: :patch) do
    '<input type="text" name="post[comment][dont_exist_on_model]">'
  end

  assert_dom_equal expected, output_buffer
end
test_fields_with_attributes_not_on_model_deep_nested() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 956
def test_fields_with_attributes_not_on_model_deep_nested
  @comment.save
  form_with(scope: :posts) do |f|
    f.fields("post[]", model: @post) do |f2|
      f2.text_field(:id)
      @post.comments.each do |comment|
        concat f2.fields("comment[]", model: comment) { |c|
          concat c.text_field(:dont_exist_on_model)
        }
      end
    end
  end

  expected = whole_form do
    '<input name="posts[post][0][comment][1][dont_exist_on_model]" type="text">'
  end

  assert_dom_equal expected, output_buffer
end
test_fields_with_file_field_generate_multipart() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 596
def test_fields_with_file_field_generate_multipart
  form_with(model: @post) do |f|
    concat f.fields(:comment, model: @post) { |c|
      concat c.file_field(:file)
    }
  end

  expected = whole_form("/posts/123", method: "patch", multipart: true) do
    "<input name='post[comment][file]' type='file' />"
  end

  assert_dom_equal expected, output_buffer
end
test_fields_with_index() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1768
def test_fields_with_index
  output_buffer = fields("post[]", model: @post) do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected =
    "<input name='post[123][title]' type='text' value='Hello World' />" \
    "<textarea name='post[123][body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[123][secret]' type='hidden' value='0' />" \
    "<input name='post[123][secret]' checked='checked' type='checkbox' value='1' />"

  assert_dom_equal expected, output_buffer
end
test_fields_with_index_option_override() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1800
def test_fields_with_index_option_override
  output_buffer = fields("post[]", model: @post, index: "abc") do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected =
    "<input name='post[abc][title]' type='text' value='Hello World' />" \
    "<textarea name='post[abc][body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[abc][secret]' type='hidden' value='0' />" \
    "<input name='post[abc][secret]' checked='checked' type='checkbox' value='1' />"

  assert_dom_equal expected, output_buffer
end
test_fields_with_labelled_builder() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2015
def test_fields_with_labelled_builder
  output_buffer = fields(:post, model: @post, builder: LabelledFormBuilder) do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected =
    "<label for='title'>Title:</label> <input name='post[title]' type='text' value='Hello World' /><br/>" \
    "<label for='body'>Body:</label> <textarea name='post[body]'>\nBack to the hill and over it again!</textarea><br/>" \
    "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' value='1' /><br/>"

  assert_dom_equal expected, output_buffer
end
test_fields_with_nil_index_option_override() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1784
def test_fields_with_nil_index_option_override
  output_buffer = fields("post[]", model: @post, index: nil) do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected =
    "<input name='post[][title]' type='text' value='Hello World' />" \
    "<textarea name='post[][body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[][secret]' type='hidden' value='0' />" \
    "<input name='post[][secret]' checked='checked' type='checkbox' value='1' />"

  assert_dom_equal expected, output_buffer
end
test_fields_with_only_object() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1832
def test_fields_with_only_object
  output_buffer = fields(model: @post) do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected =
    "<input name='post[title]' type='text' value='Hello World' />" \
    "<textarea name='post[body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[secret]' type='hidden' value='0' />" \
    "<input name='post[secret]' checked='checked' type='checkbox' value='1' />"

  assert_dom_equal expected, output_buffer
end
test_fields_without_object() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1816
def test_fields_without_object
  output_buffer = fields(:post) do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected =
    "<input name='post[title]' type='text' value='Hello World' />" \
    "<textarea name='post[body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[secret]' type='hidden' value='0' />" \
    "<input name='post[secret]' checked='checked' type='checkbox' value='1' />"

  assert_dom_equal expected, output_buffer
end
test_form_builder_does_not_have_form_with_method() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1870
def test_form_builder_does_not_have_form_with_method
  assert_not_includes ActionView::Helpers::FormBuilder.instance_methods, :form_with
end
test_form_builder_override() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1991
def test_form_builder_override
  self.default_form_builder = LabelledFormBuilder

  output_buffer = fields(:post, model: @post) do |f|
    concat f.text_field(:title)
  end

  expected = "<label for='title'>Title:</label> <input name='post[title]' type='text' value='Hello World' /><br/>"

  assert_dom_equal expected, output_buffer
end
test_form_is_not_remote_by_default_if_form_with_generates_remote_forms_is_false() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 736
def test_form_is_not_remote_by_default_if_form_with_generates_remote_forms_is_false
  old_value = ActionView::Helpers::FormHelper.form_with_generates_remote_forms
  ActionView::Helpers::FormHelper.form_with_generates_remote_forms = false

  form_with(model: @post, url: "/", id: "create-post", method: :patch) do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected = whole_form("/", "create-post", method: "patch", local: true) do
    "<input name='post[title]' type='text' value='Hello World' />" \
    "<textarea name='post[body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[secret]' type='hidden' value='0' />" \
    "<input name='post[secret]' checked='checked' type='checkbox' value='1' />"
  end

  assert_dom_equal expected, output_buffer
ensure
  ActionView::Helpers::FormHelper.form_with_generates_remote_forms = old_value
end
test_form_with() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 301
def test_form_with
  form_with(model: @post, id: "create-post") do |f|
    concat f.label(:title) { "The Title" }
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
    concat f.select(:category, %w( animal economy sports ))
    concat f.submit("Create post")
    concat f.button("Create post")
    concat f.button {
      concat content_tag(:span, "Create post")
    }
  end

  expected = whole_form("/posts/123", "create-post", method: "patch") do
    "<label for='post_title'>The Title</label>" \
    "<input name='post[title]' type='text' value='Hello World' />" \
    "<textarea name='post[body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[secret]' type='hidden' value='0' />" \
    "<input name='post[secret]' checked='checked' type='checkbox' value='1' />" \
    "<select name='post[category]'><option value='animal'>animal</option>\n<option value='economy'>economy</option>\n<option value='sports'>sports</option></select>" \
    "<input name='commit' data-disable-with='Create post' type='submit' value='Create post' />" \
    "<button name='button' type='submit'>Create post</button>" \
    "<button name='button' type='submit'><span>Create post</span></button>"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_and_fields() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1874
def test_form_with_and_fields
  form_with(model: @post, scope: :post, id: "create-post") do |post_form|
    concat post_form.text_field(:title)
    concat post_form.text_area(:body)

    concat fields(:parent_post, model: @post) { |parent_fields|
      concat parent_fields.check_box(:secret)
    }
  end

  expected = whole_form("/posts/123", "create-post", method: "patch") do
    "<input name='post[title]' type='text' value='Hello World' />" \
    "<textarea name='post[body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='parent_post[secret]' type='hidden' value='0' />" \
    "<input name='parent_post[secret]' checked='checked' type='checkbox' value='1' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_and_fields_with_non_nested_association_and_without_object() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1913
def test_form_with_and_fields_with_non_nested_association_and_without_object
  form_with(model: @post) do |f|
    concat f.fields(:category) { |c|
      concat c.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<input name='post[category][name]' type='text' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_and_fields_with_object() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1894
def test_form_with_and_fields_with_object
  form_with(model: @post, scope: :post, id: "create-post") do |post_form|
    concat post_form.text_field(:title)
    concat post_form.text_area(:body)

    concat post_form.fields(model: @comment) { |comment_fields|
      concat comment_fields.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", "create-post", method: "patch") do
    "<input name='post[title]' type='text' value='Hello World' />" \
    "<textarea name='post[body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[comment][name]' type='text' value='new comment' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_attribute_not_on_model() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 370
def test_form_with_attribute_not_on_model
  form_with(model: @post) do |f|
    concat f.text_field :this_dont_exist_on_post
  end

  expected = whole_form("/posts/123", method: :patch) do
    '<input type="text" name="post[this_dont_exist_on_post]">'
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_doesnt_call_private_or_protected_properties_on_form_object_skipping_value() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 382
def test_form_with_doesnt_call_private_or_protected_properties_on_form_object_skipping_value
  obj = Class.new do
    private def private_property
      "That would be great."
    end

    protected def protected_property
      "I believe you have my stapler."
    end
  end.new

  form_with(model: obj, scope: "other_name", url: "/", id: "edit-other-name") do |f|
    assert_dom_equal '<input type="hidden" name="other_name[private_property]">', f.hidden_field(:private_property)
    assert_dom_equal '<input type="hidden" name="other_name[protected_property]">', f.hidden_field(:protected_property)
  end
end
test_form_with_enables_remote_by_default() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 719
def test_form_with_enables_remote_by_default
  form_with(model: @post, url: "/", id: "create-post", method: :patch) do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected = whole_form("/", "create-post", method: "patch") do
    "<input name='post[title]' type='text' value='Hello World' />" \
    "<textarea name='post[body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[secret]' type='hidden' value='0' />" \
    "<input name='post[secret]' checked='checked' type='checkbox' value='1' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_general_attributes() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 358
def test_form_with_general_attributes
  form_with(url: "/posts/123") do |f|
    concat f.text_field :no_model_to_back_this_badboy
  end

  expected = whole_form("/posts/123") do
    '<input type="text" name="no_model_to_back_this_badboy">'
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_index_and_with_collection_check_boxes() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 566
def test_form_with_index_and_with_collection_check_boxes
  post = Post.new
  def post.tag_ids; [1]; end
  collection = [[1, "Tag 1"]]

  form_with(model: post, index: "1") do |f|
    concat f.collection_check_boxes(:tag_ids, collection, :first, :last)
  end

  expected = whole_form("/posts") do
    "<input name='post[1][tag_ids][]' type='hidden' value='' />" \
    "<input checked='checked' name='post[1][tag_ids][]' type='checkbox' value='1' id='post_1_tag_ids_1' />" \
    "<label for='post_1_tag_ids_1'>Tag 1</label>"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_label_error_wrapping() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 835
def test_form_with_label_error_wrapping
  form_with(model: @post) do |f|
    concat f.label(:author_name, class: "label")
    concat f.text_field(:author_name)
    concat f.submit("Create post")
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<div class='field_with_errors'><label for='post_author_name' class='label'>Author name</label></div>" \
    "<div class='field_with_errors'><input name='post[author_name]' type='text' value='' /></div>" \
    "<input name='commit' data-disable-with='Create post' type='submit' value='Create post' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_label_error_wrapping_block_and_non_block_versions() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 869
def test_form_with_label_error_wrapping_block_and_non_block_versions
  form_with(model: @post) do |f|
    concat f.label(:author_name, "Name", class: "label")
    concat f.label(:author_name, class: "label") { "Name" }
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<div class='field_with_errors'><label for='post_author_name' class='label'>Name</label></div>" \
    "<div class='field_with_errors'><label for='post_author_name' class='label'>Name</label></div>"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_label_error_wrapping_without_conventional_instance_variable() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 851
def test_form_with_label_error_wrapping_without_conventional_instance_variable
  post = remove_instance_variable :@post

  form_with(model: post) do |f|
    concat f.label(:author_name, class: "label")
    concat f.text_field(:author_name)
    concat f.submit("Create post")
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<div class='field_with_errors'><label for='post_author_name' class='label'>Author name</label></div>" \
    "<div class='field_with_errors'><input name='post[author_name]' type='text' value='' /></div>" \
    "<input name='commit' data-disable-with='Create post' type='submit' value='Create post' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_only_instantiates_builder_once() click to toggle source
Calls superclass method
# File actionview/test/template/form_helper/form_with_test.rb, line 2181
def test_form_with_only_instantiates_builder_once
  initialization_count = 0
  builder_class = Class.new(ActionView::Helpers::FormBuilder) do
    define_method :initialize do |*args|
      super(*args)
      initialization_count += 1
    end
  end

  form_with(model: @post, builder: builder_class) {}
  assert_equal 1, initialization_count, "form builder instantiated more than once"
end
test_form_with_only_url_on_create() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 330
def test_form_with_only_url_on_create
  form_with(url: "/posts") do |f|
    concat f.label :title, "Label me"
    concat f.text_field :title
  end

  expected = whole_form("/posts") do
    '<label for="title">Label me</label>' \
    '<input type="text" name="title">'
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_only_url_on_update() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 344
def test_form_with_only_url_on_update
  form_with(url: "/posts/123") do |f|
    concat f.label :title, "Label me"
    concat f.text_field :title
  end

  expected = whole_form("/posts/123") do
    '<label for="title">Label me</label>' \
    '<input type="text" name="title">'
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_requires_arguments() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 287
def test_form_with_requires_arguments
  error = assert_raises(ArgumentError) do
    form_for(nil, html: { id: "create-post" }) do
    end
  end
  assert_equal "First argument in form cannot contain nil or be empty", error.message

  error = assert_raises(ArgumentError) do
    form_for([nil, nil], html: { id: "create-post" }) do
    end
  end
  assert_equal "First argument in form cannot contain nil or be empty", error.message
end
test_form_with_skip_enforcing_utf8_false() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 770
def test_form_with_skip_enforcing_utf8_false
  form_with(scope: :post, skip_enforcing_utf8: false) do |f|
    concat f.text_field(:title)
  end

  expected = whole_form("/", skip_enforcing_utf8: false) do
    "<input name='post[title]' type='text' value='Hello World' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_skip_enforcing_utf8_true() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 758
def test_form_with_skip_enforcing_utf8_true
  form_with(scope: :post, skip_enforcing_utf8: true) do |f|
    concat f.text_field(:title)
  end

  expected = whole_form("/", skip_enforcing_utf8: true) do
    "<input name='post[title]' type='text' value='Hello World' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_collection_check_boxes() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 487
def test_form_with_with_collection_check_boxes
  post = Post.new
  def post.tag_ids; [1, 3]; end
  collection = (1..3).map { |i| [i, "Tag #{i}"] }
  form_with(model: post) do |f|
    concat f.collection_check_boxes(:tag_ids, collection, :first, :last)
  end

  expected = whole_form("/posts") do
    "<input name='post[tag_ids][]' type='hidden' value='' />" \
    "<input checked='checked' name='post[tag_ids][]' type='checkbox' value='1' id='post_tag_ids_1' />" \
    "<label for='post_tag_ids_1'>Tag 1</label>" \
    "<input name='post[tag_ids][]' type='checkbox' value='2' id='post_tag_ids_2' />" \
    "<label for='post_tag_ids_2'>Tag 2</label>" \
    "<input checked='checked' name='post[tag_ids][]' type='checkbox' value='3' id='post_tag_ids_3' />" \
    "<label for='post_tag_ids_3'>Tag 3</label>"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_collection_check_boxes_with_custom_builder_block() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 508
def test_form_with_with_collection_check_boxes_with_custom_builder_block
  post = Post.new
  def post.tag_ids; [1, 3]; end
  collection = (1..3).map { |i| [i, "Tag #{i}"] }
  form_with(model: post) do |f|
    rendered_check_boxes = f.collection_check_boxes(:tag_ids, collection, :first, :last) do |b|
      b.label { b.check_box + b.text }
    end
    concat rendered_check_boxes
  end

  expected = whole_form("/posts") do
    "<input name='post[tag_ids][]' type='hidden' value='' />" \
    "<label for='post_tag_ids_1'>" \
    "<input checked='checked' name='post[tag_ids][]' type='checkbox' value='1' id='post_tag_ids_1' />" \
    "Tag 1</label>" \
    "<label for='post_tag_ids_2'>" \
    "<input name='post[tag_ids][]' type='checkbox' value='2' id='post_tag_ids_2' />" \
    "Tag 2</label>" \
    "<label for='post_tag_ids_3'>" \
    "<input checked='checked' name='post[tag_ids][]' type='checkbox' value='3' id='post_tag_ids_3' />" \
    "Tag 3</label>"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_collection_check_boxes_with_custom_builder_block_does_not_leak_the_template() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 535
def test_form_with_with_collection_check_boxes_with_custom_builder_block_does_not_leak_the_template
  post = Post.new
  def post.tag_ids; [1, 3]; end
  def post.id; 1; end
  collection = (1..3).map { |i| [i, "Tag #{i}"] }

  form_with(model: post) do |f|
    rendered_check_boxes = f.collection_check_boxes(:tag_ids, collection, :first, :last) do |b|
      b.label { b.check_box + b.text }
    end
    concat rendered_check_boxes
    concat f.hidden_field :id
  end

  expected = whole_form("/posts") do
    "<input name='post[tag_ids][]' type='hidden' value='' />" \
    "<label for='post_tag_ids_1'>" \
    "<input checked='checked' name='post[tag_ids][]' type='checkbox' value='1' id='post_tag_ids_1' />" \
    "Tag 1</label>" \
    "<label for='post_tag_ids_2'>" \
    "<input name='post[tag_ids][]' type='checkbox' value='2' id='post_tag_ids_2' />" \
    "Tag 2</label>" \
    "<label for='post_tag_ids_3'>" \
    "<input checked='checked' name='post[tag_ids][]' type='checkbox' value='3' id='post_tag_ids_3' />" \
    "Tag 3</label>" \
    "<input name='post[id]' type='hidden' value='1' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_collection_radio_buttons() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 399
def test_form_with_with_collection_radio_buttons
  post = Post.new
  def post.active; false; end
  form_with(model: post) do |f|
    concat f.collection_radio_buttons(:active, [true, false], :to_s, :to_s)
  end

  expected = whole_form("/posts") do
    "<input type='hidden' name='post[active]' value='' />" \
    "<input name='post[active]' type='radio' value='true' id='post_active_true' />" \
    "<label for='post_active_true'>true</label>" \
    "<input checked='checked' name='post[active]' type='radio' value='false' id='post_active_false' />" \
    "<label for='post_active_false'>false</label>"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_collection_radio_buttons_with_custom_builder_block() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 417
def test_form_with_with_collection_radio_buttons_with_custom_builder_block
  post = Post.new
  def post.active; false; end

  form_with(model: post) do |f|
    rendered_radio_buttons = f.collection_radio_buttons(:active, [true, false], :to_s, :to_s) do |b|
      b.label { b.radio_button + b.text }
    end
    concat rendered_radio_buttons
  end

  expected = whole_form("/posts") do
    "<input type='hidden' name='post[active]' value='' />" \
    "<label for='post_active_true'>" \
    "<input name='post[active]' type='radio' value='true' id='post_active_true' />" \
    "true</label>" \
    "<label for='post_active_false'>" \
    "<input checked='checked' name='post[active]' type='radio' value='false' id='post_active_false' />" \
    "false</label>"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_collection_radio_buttons_with_custom_builder_block_does_not_leak_the_template() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 441
def test_form_with_with_collection_radio_buttons_with_custom_builder_block_does_not_leak_the_template
  post = Post.new
  def post.active; false; end
  def post.id; 1; end

  form_with(model: post) do |f|
    rendered_radio_buttons = f.collection_radio_buttons(:active, [true, false], :to_s, :to_s) do |b|
      b.label { b.radio_button + b.text }
    end
    concat rendered_radio_buttons
    concat f.hidden_field :id
  end

  expected = whole_form("/posts") do
    "<input type='hidden' name='post[active]' value='' />" \
    "<label for='post_active_true'>" \
    "<input name='post[active]' type='radio' value='true' id='post_active_true' />" \
    "true</label>" \
    "<label for='post_active_false'>" \
    "<input checked='checked' name='post[active]' type='radio' value='false' id='post_active_false' />" \
    "false</label>" \
    "<input name='post[id]' type='hidden' value='1' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_data_attributes() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2170
def test_form_with_with_data_attributes
  form_with(model: @post, data: { behavior: "stuff" }) {}
  assert_match %r|data-behavior="stuff"|, output_buffer
  assert_match %r|data-remote="true"|, output_buffer
end
test_form_with_with_default_method_as_patch() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2164
def test_form_with_with_default_method_as_patch
  form_with(model: @post) {}
  expected = whole_form("/posts/123", method: "patch")
  assert_dom_equal expected, output_buffer
end
test_form_with_with_existing_object() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2109
def test_form_with_with_existing_object
  form_with(model: @post) do |f| end

  expected = whole_form("/posts/123", method: "patch")
  assert_dom_equal expected, output_buffer
end
test_form_with_with_existing_object_and_custom_url() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2157
def test_form_with_with_existing_object_and_custom_url
  form_with(model: @post, url: "/super_posts") do |f| end

  expected = whole_form("/super_posts", method: "patch")
  assert_dom_equal expected, output_buffer
end
test_form_with_with_existing_object_and_namespace_in_list() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2142
def test_form_with_with_existing_object_and_namespace_in_list
  @comment.save
  form_with(model: [:admin, @post, @comment]) {}

  expected = whole_form(admin_post_comment_path(@post, @comment), method: "patch")
  assert_dom_equal expected, output_buffer
end
test_form_with_with_existing_object_in_list() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2127
def test_form_with_with_existing_object_in_list
  @comment.save
  form_with(model: [@post, @comment]) {}

  expected = whole_form(post_comment_path(@post, @comment), method: "patch")
  assert_dom_equal expected, output_buffer
end
test_form_with_with_file_field_generate_multipart() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 584
def test_form_with_with_file_field_generate_multipart
  form_with(model: @post, id: "create-post") do |f|
    concat f.file_field(:file)
  end

  expected = whole_form("/posts/123", "create-post", method: "patch", multipart: true) do
    "<input name='post[file]' type='file' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_format() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 610
def test_form_with_with_format
  form_with(model: @post, format: :json, id: "edit_post_123", class: "edit_post") do |f|
    concat f.label(:title)
  end

  expected = whole_form("/posts/123.json", "edit_post_123", "edit_post", method: "patch") do
    "<label for='post_title'>Title</label>"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_format_and_url() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 622
def test_form_with_with_format_and_url
  form_with(model: @post, format: :json, url: "/") do |f|
    concat f.label(:title)
  end

  expected = whole_form("/", method: "patch") do
    "<label for='post_title'>Title</label>"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_hash_url_option() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2095
def test_form_with_with_hash_url_option
  form_with(model: @post, url: { controller: "controller", action: "action" }) do |f| end

  assert_equal "controller", @url_for_options[:controller]
  assert_equal "action", @url_for_options[:action]
end
test_form_with_with_html_options_adds_options_to_form_tag() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2082
def test_form_with_with_html_options_adds_options_to_form_tag
  form_with(model: @post, html: { id: "some_form", class: "some_class", multipart: true }) do |f| end
  expected = whole_form("/posts/123", "some_form", "some_class", method: "patch", multipart: "multipart/form-data")

  assert_dom_equal expected, output_buffer
end
test_form_with_with_index() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 799
def test_form_with_with_index
  form_with(model: @post, scope: "post[]") do |f|
    concat f.label(:title)
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<label for='post_123_title'>Title</label>" \
    "<input name='post[123][title]' type='text' value='Hello World' />" \
    "<textarea name='post[123][body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[123][secret]' type='hidden' value='0' />" \
    "<input name='post[123][secret]' checked='checked' type='checkbox' value='1' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_index_and_nested_fields() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1043
def test_form_with_with_index_and_nested_fields
  output_buffer = form_with(model: @post, index: 1) do |f|
    concat f.fields(:comment, model: @post) { |c|
      concat c.text_field(:title)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<input name='post[1][comment][title]' type='text' value='Hello World' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_index_and_with_collection_radio_buttons() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 468
def test_form_with_with_index_and_with_collection_radio_buttons
  post = Post.new
  def post.active; false; end

  form_with(model: post, index: "1") do |f|
    concat f.collection_radio_buttons(:active, [true, false], :to_s, :to_s)
  end

  expected = whole_form("/posts") do
    "<input type='hidden' name='post[1][active]' value='' />" \
    "<input name='post[1][active]' type='radio' value='true' id='post_1_active_true' />" \
    "<label for='post_1_active_true'>true</label>" \
    "<input checked='checked' name='post[1][active]' type='radio' value='false' id='post_1_active_false' />" \
    "<label for='post_1_active_false'>false</label>"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_labelled_builder() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1937
def test_form_with_with_labelled_builder
  form_with(model: @post, builder: LabelledFormBuilder) do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<label for='title'>Title:</label> <input name='post[title]' type='text' value='Hello World' /><br/>" \
    "<label for='body'>Body:</label> <textarea name='post[body]'>\nBack to the hill and over it again!</textarea><br/>" \
    "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' value='1' /><br/>"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_labelled_builder_path() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2056
def test_form_with_with_labelled_builder_path
  path = nil

  form_with(model: @post, builder: LabelledFormBuilder) do |f|
    path = f.to_partial_path
    ""
  end

  assert_equal "labelled_form", path
end
test_form_with_with_labelled_builder_with_nested_fields_with_custom_builder() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2069
def test_form_with_with_labelled_builder_with_nested_fields_with_custom_builder
  klass = nil

  form_with(model: @post, builder: LabelledFormBuilder) do |f|
    f.fields(:comments, model: Comment.new, builder: LabelledFormBuilderSubclass) do |nested_fields|
      klass = nested_fields.class
      ""
    end
  end

  assert_equal LabelledFormBuilderSubclass, klass
end
test_form_with_with_labelled_builder_with_nested_fields_with_options_hash() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2043
def test_form_with_with_labelled_builder_with_nested_fields_with_options_hash
  klass = nil

  form_with(model: @post, builder: LabelledFormBuilder) do |f|
    f.fields(:comments, model: Comment.new, index: "foo") do |nested_fields|
      klass = nested_fields.class
      ""
    end
  end

  assert_equal LabelledFormBuilder, klass
end
test_form_with_with_labelled_builder_with_nested_fields_without_options_hash() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2030
def test_form_with_with_labelled_builder_with_nested_fields_without_options_hash
  klass = nil

  form_with(model: @post, builder: LabelledFormBuilder) do |f|
    f.fields(:comments, model: Comment.new) do |nested_fields|
      klass = nested_fields.class
      ""
    end
  end

  assert_equal LabelledFormBuilder, klass
end
test_form_with_with_method() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 688
def test_form_with_with_method
  form_with(model: @post, url: "/", method: :delete, id: "create-post") do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected = whole_form("/", "create-post", method: "delete") do
    "<input name='post[title]' type='text' value='Hello World' />" \
    "<textarea name='post[body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[secret]' type='hidden' value='0' />" \
    "<input name='post[secret]' checked='checked' type='checkbox' value='1' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_method_as_part_of_html_options() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 671
def test_form_with_with_method_as_part_of_html_options
  form_with(model: @post, url: "/", id: "create-post", html: { method: :delete }) do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected = whole_form("/", "create-post", method: "delete") do
    "<input name='post[title]' type='text' value='Hello World' />" \
    "<textarea name='post[body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[secret]' type='hidden' value='0' />" \
    "<input name='post[secret]' checked='checked' type='checkbox' value='1' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_model_using_relative_model_naming() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 634
def test_form_with_with_model_using_relative_model_naming
  blog_post = Blog::Post.new("And his name will be forty and four.", 44)

  form_with(model: blog_post) do |f|
    concat f.text_field :title
    concat f.submit("Edit post")
  end

  expected = whole_form("/posts/44", method: "patch") do
    "<input name='post[title]' type='text' value='And his name will be forty and four.' />" \
    "<input name='commit' data-disable-with='Edit post' type='submit' value='Edit post' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_new_object() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2116
def test_form_with_with_new_object
  post = Post.new
  post.persisted = false
  def post.to_key; nil; end

  form_with(model: post) {}

  expected = whole_form("/posts")
  assert_dom_equal expected, output_buffer
end
test_form_with_with_new_object_and_namespace_in_list() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2150
def test_form_with_with_new_object_and_namespace_in_list
  form_with(model: [:admin, @post, @comment]) {}

  expected = whole_form(admin_post_comments_path(@post))
  assert_dom_equal expected, output_buffer
end
test_form_with_with_new_object_in_list() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2135
def test_form_with_with_new_object_in_list
  form_with(model: [@post, @comment]) {}

  expected = whole_form(post_comments_path(@post))
  assert_dom_equal expected, output_buffer
end
test_form_with_with_nil_index_option_override() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 818
def test_form_with_with_nil_index_option_override
  form_with(model: @post, scope: "post[]", index: nil) do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<input name='post[][title]' type='text' value='Hello World' />" \
    "<textarea name='post[][body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[][secret]' type='hidden' value='0' />" \
    "<input name='post[][secret]' checked='checked' type='checkbox' value='1' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_record_url_option() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2102
def test_form_with_with_record_url_option
  form_with(model: @post, url: @post) do |f| end

  expected = whole_form("/posts/123", method: "patch")
  assert_dom_equal expected, output_buffer
end
test_form_with_with_search_field() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 705
def test_form_with_with_search_field
  # Test case for bug which would emit an "object" attribute
  # when used with form_for using a search_field form helper
  form_with(model: Post.new, url: "/search", id: "search-post", method: :get) do |f|
    concat f.search_field(:title)
  end

  expected = whole_form("/search", "search-post", method: "get") do
    "<input name='post[title]' type='search' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_with_string_url_option() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2089
def test_form_with_with_string_url_option
  form_with(model: @post, url: "http://www.otherdomain.com") do |f| end

  assert_dom_equal whole_form("http://www.otherdomain.com", method: "patch"), output_buffer
end
test_form_with_with_symbol_scope() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 650
def test_form_with_with_symbol_scope
  form_with(model: @post, scope: "other_name", id: "create-post") do |f|
    concat f.label(:title, class: "post_title")
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
    concat f.submit("Create post")
  end

  expected = whole_form("/posts/123", "create-post", method: "patch") do
    "<label for='other_name_title' class='post_title'>Title</label>" \
    "<input name='other_name[title]' value='Hello World' type='text' />" \
    "<textarea name='other_name[body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='other_name[secret]' value='0' type='hidden' />" \
    "<input name='other_name[secret]' checked='checked' value='1' type='checkbox' />" \
    "<input name='commit' value='Create post' data-disable-with='Create post' type='submit' />"
  end

  assert_dom_equal expected, output_buffer
end
test_form_with_without_object() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 782
def test_form_with_without_object
  form_with(scope: :post, id: "create-post") do |f|
    concat f.text_field(:title)
    concat f.text_area(:body)
    concat f.check_box(:secret)
  end

  expected = whole_form("/", "create-post") do
    "<input name='post[title]' type='text' value='Hello World' />" \
    "<textarea name='post[body]'>\nBack to the hill and over it again!</textarea>" \
    "<input name='post[secret]' type='hidden' value='0' />" \
    "<input name='post[secret]' checked='checked' type='checkbox' value='1' />"
  end

  assert_dom_equal expected, output_buffer
end
test_lazy_loading_default_form_builder() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1974
def test_lazy_loading_default_form_builder
  old_default_form_builder, ActionView::Base.default_form_builder =
    ActionView::Base.default_form_builder, "FormWithActsLikeFormForTest::LabelledFormBuilder"

  form_with(model: @post) do |f|
    concat f.text_field(:title)
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<label for='title'>Title:</label> <input name='post[title]' type='text' value='Hello World' /><br/>"
  end

  assert_dom_equal expected, output_buffer
ensure
  ActionView::Base.default_form_builder = old_default_form_builder
end
test_lazy_loading_form_builder_override() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2003
def test_lazy_loading_form_builder_override
  self.default_form_builder = "FormWithActsLikeFormForTest::LabelledFormBuilder"

  output_buffer = fields(:post, model: @post) do |f|
    concat f.text_field(:title)
  end

  expected = "<label for='title'>Title:</label> <input name='post[title]' type='text' value='Hello World' /><br/>"

  assert_dom_equal expected, output_buffer
end
test_nested_fields() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 976
def test_nested_fields
  @comment.body = "Hello World"
  form_with(model: @post) do |f|
    concat f.fields(model: @comment) { |c|
      concat c.text_field(:body)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<input name='post[comment][body]' type='text' value='Hello World' />"
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_arel_like() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1504
def test_nested_fields_arel_like
  @post.comments = ArelLike.new

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    concat f.fields(:comments, model: @post.comments) { |cf|
      concat cf.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[comments_attributes][0][name]" type="text" value="comment #1" />' \
    '<input name="post[comments_attributes][0][id]" type="hidden" value="1" />' \
    '<input name="post[comments_attributes][1][name]" type="text" value="comment #2" />' \
    '<input name="post[comments_attributes][1][id]" type="hidden" value="2" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_index_method_with_child_index_option_override_on_a_nested_attributes_collection_association() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1680
def test_nested_fields_index_method_with_child_index_option_override_on_a_nested_attributes_collection_association
  @post.comments = []

  form_with(model: @post) do |f|
    f.fields(:comments, model: Comment.new(321), child_index: "abc") { |cf|
      assert_equal "abc", cf.index
    }
  end
end
test_nested_fields_index_method_with_existing_and_new_records_on_a_nested_attributes_collection_association() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1654
def test_nested_fields_index_method_with_existing_and_new_records_on_a_nested_attributes_collection_association
  @post.comments = [Comment.new(321), Comment.new]

  form_with(model: @post) do |f|
    expected = 0
    @post.comments.each do |comment|
      f.fields(:comments, model: comment) { |cf|
        assert_equal expected, cf.index
        expected += 1
      }
    end
  end
end
test_nested_fields_index_method_with_existing_records_on_a_nested_attributes_collection_association() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1640
def test_nested_fields_index_method_with_existing_records_on_a_nested_attributes_collection_association
  @post.comments = Array.new(2) { |id| Comment.new(id + 1) }

  form_with(model: @post) do |f|
    expected = 0
    @post.comments.each do |comment|
      f.fields(:comments, model: comment) { |cf|
        assert_equal expected, cf.index
        expected += 1
      }
    end
  end
end
test_nested_fields_index_method_with_existing_records_on_a_supplied_nested_attributes_collection() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1668
def test_nested_fields_index_method_with_existing_records_on_a_supplied_nested_attributes_collection
  @post.comments = Array.new(2) { |id| Comment.new(id + 1) }

  form_with(model: @post) do |f|
    expected = 0
    f.fields(:comments, model: @post.comments) { |cf|
      assert_equal expected, cf.index
      expected += 1
    }
  end
end
test_nested_fields_label_translation_with_more_than_10_records() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1525
def test_nested_fields_label_translation_with_more_than_10_records
  @post.comments = Array.new(11) { |id| Comment.new(id + 1) }

  params = 11.times.map { ["post.comments.body", default: [:"comment.body", ""], scope: "helpers.label"] }
  assert_called_with(I18n, :t, params, returns: "Write body here") do
    form_with(model: @post) do |f|
      f.fields(:comments) do |cf|
        concat cf.label(:body)
      end
    end
  end
end
test_nested_fields_on_a_nested_attributes_collection_association_yields_only_builder() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1560
def test_nested_fields_on_a_nested_attributes_collection_association_yields_only_builder
  @post.comments = [Comment.new(321), Comment.new]
  yielded_comments = []

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    concat f.fields(:comments) { |cf|
      concat cf.text_field(:name)
      yielded_comments << cf.object
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[comments_attributes][0][name]" type="text" value="comment #321" />' \
    '<input name="post[comments_attributes][0][id]" type="hidden" value="321" />' \
    '<input name="post[comments_attributes][1][name]" type="text" value="new comment" />'
  end

  assert_dom_equal expected, output_buffer
  assert_equal yielded_comments, @post.comments
end
test_nested_fields_uses_unique_indices_for_different_collection_associations() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1690
def test_nested_fields_uses_unique_indices_for_different_collection_associations
  @post.comments = [Comment.new(321)]
  @post.tags = [Tag.new(123), Tag.new(456)]
  @post.comments[0].relevances = []
  @post.tags[0].relevances = []
  @post.tags[1].relevances = []

  form_with(model: @post) do |f|
    concat f.fields(:comments, model: @post.comments[0]) { |cf|
      concat cf.text_field(:name)
      concat cf.fields(:relevances, model: CommentRelevance.new(314)) { |crf|
        concat crf.text_field(:value)
      }
    }
    concat f.fields(:tags, model: @post.tags[0]) { |tf|
      concat tf.text_field(:value)
      concat tf.fields(:relevances, model: TagRelevance.new(3141)) { |trf|
        concat trf.text_field(:value)
      }
    }
    concat f.fields("tags", model: @post.tags[1]) { |tf|
      concat tf.text_field(:value)
      concat tf.fields(:relevances, model: TagRelevance.new(31415)) { |trf|
        concat trf.text_field(:value)
      }
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[comments_attributes][0][name]" type="text" value="comment #321" />' \
    '<input name="post[comments_attributes][0][relevances_attributes][0][value]" type="text" value="commentrelevance #314" />' \
    '<input name="post[comments_attributes][0][relevances_attributes][0][id]" type="hidden" value="314" />' \
    '<input name="post[comments_attributes][0][id]" type="hidden" value="321" />' \
    '<input name="post[tags_attributes][0][value]" type="text" value="tag #123" />' \
    '<input name="post[tags_attributes][0][relevances_attributes][0][value]" type="text" value="tagrelevance #3141" />' \
    '<input name="post[tags_attributes][0][relevances_attributes][0][id]" type="hidden" value="3141" />' \
    '<input name="post[tags_attributes][0][id]" type="hidden" value="123" />' \
    '<input name="post[tags_attributes][1][value]" type="text" value="tag #456" />' \
    '<input name="post[tags_attributes][1][relevances_attributes][0][value]" type="text" value="tagrelevance #31415" />' \
    '<input name="post[tags_attributes][1][relevances_attributes][0][id]" type="hidden" value="31415" />' \
    '<input name="post[tags_attributes][1][id]" type="hidden" value="456" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_a_new_record_on_a_nested_attributes_one_to_one_association() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1135
def test_nested_fields_with_a_new_record_on_a_nested_attributes_one_to_one_association
  @post.author = Author.new

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    concat f.fields(:author) { |af|
      concat af.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
      '<input name="post[author_attributes][name]" type="text" value="new author" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_an_empty_supplied_attributes_collection() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1468
def test_nested_fields_with_an_empty_supplied_attributes_collection
  form_with(model: @post) do |f|
    concat f.text_field(:title)
    f.fields(:comments, model: []) do |cf|
      concat cf.text_field(:name)
    end
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_an_existing_record_on_a_nested_attributes_one_to_one_association() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1162
def test_nested_fields_with_an_existing_record_on_a_nested_attributes_one_to_one_association
  @post.author = Author.new(321)

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    concat f.fields(:author) { |af|
      concat af.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[author_attributes][name]" type="text" value="author #321" />' \
    '<input name="post[author_attributes][id]" type="hidden" value="321" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_an_existing_record_on_a_nested_attributes_one_to_one_association_using_erb_and_inline_block() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1181
def test_nested_fields_with_an_existing_record_on_a_nested_attributes_one_to_one_association_using_erb_and_inline_block
  @post.author = Author.new(321)

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    concat f.fields(:author) { |af|
      af.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[author_attributes][name]" type="text" value="author #321" />' \
    '<input name="post[author_attributes][id]" type="hidden" value="321" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_an_existing_record_on_a_nested_attributes_one_to_one_association_with_disabled_hidden_id() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1200
def test_nested_fields_with_an_existing_record_on_a_nested_attributes_one_to_one_association_with_disabled_hidden_id
  @post.author = Author.new(321)

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    concat f.fields(:author, skip_id: true) { |af|
      af.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[author_attributes][name]" type="text" value="author #321" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_an_existing_record_on_a_nested_attributes_one_to_one_association_with_disabled_hidden_id_inherited() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1218
def test_nested_fields_with_an_existing_record_on_a_nested_attributes_one_to_one_association_with_disabled_hidden_id_inherited
  @post.author = Author.new(321)

  form_with(model: @post, skip_id: true) do |f|
    concat f.text_field(:title)
    concat f.fields(:author) { |af|
      af.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[author_attributes][name]" type="text" value="author #321" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_an_existing_record_on_a_nested_attributes_one_to_one_association_with_disabled_hidden_id_override() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1236
def test_nested_fields_with_an_existing_record_on_a_nested_attributes_one_to_one_association_with_disabled_hidden_id_override
  @post.author = Author.new(321)

  form_with(model: @post, skip_id: true) do |f|
    concat f.text_field(:title)
    concat f.fields(:author, skip_id: false) { |af|
      af.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[author_attributes][name]" type="text" value="author #321" />' \
    '<input name="post[author_attributes][id]" type="hidden" value="321" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_auto_index() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1071
def test_nested_fields_with_auto_index
  form_with(model: @post, scope: "post[]") do |f|
    concat f.fields(:comment, model: @post) { |c|
      concat c.text_field(:title)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<input name='post[123][comment][title]' type='text' value='Hello World' />"
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_auto_index_on_both() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1099
def test_nested_fields_with_auto_index_on_both
  form_with(model: @post, scope: "post[]") do |f|
    concat f.fields("comment[]", model: @post) { |c|
      concat c.text_field(:title)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<input name='post[123][comment][123][title]' type='text' value='Hello World' />"
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_child_index_as_lambda_option_override_on_a_nested_attributes_collection_association() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1600
def test_nested_fields_with_child_index_as_lambda_option_override_on_a_nested_attributes_collection_association
  @post.comments = []

  form_with(model: @post) do |f|
    concat f.fields(:comments, model: Comment.new(321), child_index: -> { "abc" }) { |cf|
      concat cf.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[comments_attributes][abc][name]" type="text" value="comment #321" />' \
    '<input name="post[comments_attributes][abc][id]" type="hidden" value="321" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_child_index_option_override_on_a_nested_attributes_collection_association() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1583
def test_nested_fields_with_child_index_option_override_on_a_nested_attributes_collection_association
  @post.comments = []

  form_with(model: @post) do |f|
    concat f.fields(:comments, model: Comment.new(321), child_index: "abc") { |cf|
      concat cf.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[comments_attributes][abc][name]" type="text" value="comment #321" />' \
    '<input name="post[comments_attributes][abc][id]" type="hidden" value="321" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_child_index_option_override_on_a_nested_attributes_collection_association_with_proxy() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1623
def test_nested_fields_with_child_index_option_override_on_a_nested_attributes_collection_association_with_proxy
  @post.comments = FakeAssociationProxy.new

  form_with(model: @post) do |f|
    concat f.fields(:comments, model: Comment.new(321), child_index: "abc") { |cf|
      concat cf.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[comments_attributes][abc][name]" type="text" value="comment #321" />' \
    '<input name="post[comments_attributes][abc][id]" type="hidden" value="321" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_existing_and_new_records_on_a_nested_attributes_collection_association() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1446
def test_nested_fields_with_existing_and_new_records_on_a_nested_attributes_collection_association
  @post.comments = [Comment.new(321), Comment.new]

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    @post.comments.each do |comment|
      concat f.fields(:comments, model: comment) { |cf|
        concat cf.text_field(:name)
      }
    end
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[comments_attributes][0][name]" type="text" value="comment #321" />' \
    '<input name="post[comments_attributes][0][id]" type="hidden" value="321" />' \
    '<input name="post[comments_attributes][1][name]" type="text" value="new comment" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_existing_records_on_a_nested_attributes_collection_association() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1275
def test_nested_fields_with_existing_records_on_a_nested_attributes_collection_association
  @post.comments = Array.new(2) { |id| Comment.new(id + 1) }

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    @post.comments.each do |comment|
      concat f.fields(:comments, model: comment) { |cf|
        concat cf.text_field(:name)
      }
    end
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[comments_attributes][0][name]" type="text" value="comment #1" />' \
    '<input name="post[comments_attributes][0][id]" type="hidden" value="1" />' \
    '<input name="post[comments_attributes][1][name]" type="text" value="comment #2" />' \
    '<input name="post[comments_attributes][1][id]" type="hidden" value="2" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_existing_records_on_a_nested_attributes_collection_association_using_erb_and_inline_block() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1378
def test_nested_fields_with_existing_records_on_a_nested_attributes_collection_association_using_erb_and_inline_block
  @post.comments = Array.new(2) { |id| Comment.new(id + 1) }

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    @post.comments.each do |comment|
      concat f.fields(:comments, model: comment) { |cf|
        cf.text_field(:name)
      }
    end
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[comments_attributes][0][name]" type="text" value="comment #1" />' \
    '<input name="post[comments_attributes][0][id]" type="hidden" value="1" />' \
    '<input name="post[comments_attributes][1][name]" type="text" value="comment #2" />' \
    '<input name="post[comments_attributes][1][id]" type="hidden" value="2" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_existing_records_on_a_nested_attributes_collection_association_with_disabled_hidden_id() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1298
def test_nested_fields_with_existing_records_on_a_nested_attributes_collection_association_with_disabled_hidden_id
  @post.comments = Array.new(2) { |id| Comment.new(id + 1) }
  @post.author = Author.new(321)

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    concat f.fields(:author) { |af|
      concat af.text_field(:name)
    }
    @post.comments.each do |comment|
      concat f.fields(:comments, model: comment, skip_id: true) { |cf|
        concat cf.text_field(:name)
      }
    end
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[author_attributes][name]" type="text" value="author #321" />' \
    '<input name="post[author_attributes][id]" type="hidden" value="321" />' \
    '<input name="post[comments_attributes][0][name]" type="text" value="comment #1" />' \
    '<input name="post[comments_attributes][1][name]" type="text" value="comment #2" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_existing_records_on_a_nested_attributes_collection_association_with_disabled_hidden_id_inherited() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1325
def test_nested_fields_with_existing_records_on_a_nested_attributes_collection_association_with_disabled_hidden_id_inherited
  @post.comments = Array.new(2) { |id| Comment.new(id + 1) }
  @post.author = Author.new(321)

  form_with(model: @post, skip_id: true) do |f|
    concat f.text_field(:title)
    concat f.fields(:author) { |af|
      concat af.text_field(:name)
    }
    @post.comments.each do |comment|
      concat f.fields(:comments, model: comment) { |cf|
        concat cf.text_field(:name)
      }
    end
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[author_attributes][name]" type="text" value="author #321" />' \
    '<input name="post[comments_attributes][0][name]" type="text" value="comment #1" />' \
    '<input name="post[comments_attributes][1][name]" type="text" value="comment #2" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_existing_records_on_a_nested_attributes_collection_association_with_disabled_hidden_id_override() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1351
def test_nested_fields_with_existing_records_on_a_nested_attributes_collection_association_with_disabled_hidden_id_override
  @post.comments = Array.new(2) { |id| Comment.new(id + 1) }
  @post.author = Author.new(321)

  form_with(model: @post, skip_id: true) do |f|
    concat f.text_field(:title)
    concat f.fields(:author, skip_id: false) { |af|
      concat af.text_field(:name)
    }
    @post.comments.each do |comment|
      concat f.fields(:comments, model: comment) { |cf|
        concat cf.text_field(:name)
      }
    end
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[author_attributes][name]" type="text" value="author #321" />' \
    '<input name="post[author_attributes][id]" type="hidden" value="321" />' \
    '<input name="post[comments_attributes][0][name]" type="text" value="comment #1" />' \
    '<input name="post[comments_attributes][1][name]" type="text" value="comment #2" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_existing_records_on_a_nested_attributes_collection_association_with_explicit_hidden_field_placement() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1401
def test_nested_fields_with_existing_records_on_a_nested_attributes_collection_association_with_explicit_hidden_field_placement
  @post.comments = Array.new(2) { |id| Comment.new(id + 1) }

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    @post.comments.each do |comment|
      concat f.fields(:comments, model: comment) { |cf|
        concat cf.hidden_field(:id)
        concat cf.text_field(:name)
      }
    end
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[comments_attributes][0][id]" type="hidden" value="1" />' \
    '<input name="post[comments_attributes][0][name]" type="text" value="comment #1" />' \
    '<input name="post[comments_attributes][1][id]" type="hidden" value="2" />' \
    '<input name="post[comments_attributes][1][name]" type="text" value="comment #2" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_existing_records_on_a_nested_attributes_one_to_one_association_with_explicit_hidden_field_placement() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1255
def test_nested_fields_with_existing_records_on_a_nested_attributes_one_to_one_association_with_explicit_hidden_field_placement
  @post.author = Author.new(321)

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    concat f.fields(:author) { |af|
      concat af.hidden_field(:id)
      concat af.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[author_attributes][id]" type="hidden" value="321" />' \
    '<input name="post[author_attributes][name]" type="text" value="author #321" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_existing_records_on_a_supplied_nested_attributes_collection() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1483
def test_nested_fields_with_existing_records_on_a_supplied_nested_attributes_collection
  @post.comments = Array.new(2) { |id| Comment.new(id + 1) }

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    concat f.fields(:comments, model: @post.comments) { |cf|
      concat cf.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[comments_attributes][0][name]" type="text" value="comment #1" />' \
    '<input name="post[comments_attributes][0][id]" type="hidden" value="1" />' \
    '<input name="post[comments_attributes][1][name]" type="text" value="comment #2" />' \
    '<input name="post[comments_attributes][1][id]" type="hidden" value="2" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_existing_records_on_a_supplied_nested_attributes_collection_different_from_record_one() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1538
def test_nested_fields_with_existing_records_on_a_supplied_nested_attributes_collection_different_from_record_one
  comments = Array.new(2) { |id| Comment.new(id + 1) }
  @post.comments = []

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    concat f.fields(:comments, model: comments) { |cf|
      concat cf.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[comments_attributes][0][name]" type="text" value="comment #1" />' \
    '<input name="post[comments_attributes][0][id]" type="hidden" value="1" />' \
    '<input name="post[comments_attributes][1][name]" type="text" value="comment #2" />' \
    '<input name="post[comments_attributes][1][id]" type="hidden" value="2" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_explicitly_passed_object_on_a_nested_attributes_one_to_one_association() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1153
def test_nested_fields_with_explicitly_passed_object_on_a_nested_attributes_one_to_one_association
  form_with(model: @post) do |f|
    f.fields(:author, model: Author.new(123)) do |af|
      assert_not_nil af.object
      assert_equal 123, af.object.id
    end
  end
end
test_nested_fields_with_hash_like_model() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1736
def test_nested_fields_with_hash_like_model
  @author = HashBackedAuthor.new

  form_with(model: @post) do |f|
    concat f.fields(:author, model: @author) { |af|
      concat af.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[author_attributes][name]" type="text" value="hash backed author" />'
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_index_and_auto_index() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1113
def test_nested_fields_with_index_and_auto_index
  output_buffer = form_with(model: @post, scope: "post[]") do |f|
    concat f.fields(:comment, model: @post, index: 5) { |c|
      concat c.text_field(:title)
    }
  end

  output_buffer << form_with(model: @post, index: 1) do |f|
    concat f.fields("comment[]", model: @post) { |c|
      concat c.text_field(:title)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<input name='post[123][comment][5][title]' type='text' value='Hello World' />"
  end + whole_form("/posts/123", method: "patch") do
    "<input name='post[1][comment][123][title]' type='text' value='Hello World' />"
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_index_and_parent_fields() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1027
def test_nested_fields_with_index_and_parent_fields
  form_with(model: @post, index: 1) do |c|
    concat c.text_field(:title)
    concat c.fields("comment", model: @comment, index: 1) { |r|
      concat r.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<input name='post[1][title]' type='text' value='Hello World' />" \
    "<input name='post[1][comment][1][name]' type='text' value='new comment' />"
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_index_on_both() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1057
def test_nested_fields_with_index_on_both
  form_with(model: @post, index: 1) do |f|
    concat f.fields(:comment, model: @post, index: 5) { |c|
      concat c.text_field(:title)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<input name='post[1][comment][5][title]' type='text' value='Hello World' />"
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_index_radio_button() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1085
def test_nested_fields_with_index_radio_button
  form_with(model: @post) do |f|
    concat f.fields(:comment, model: @post, index: 5) { |c|
      concat c.radio_button(:title, "hello")
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<input name='post[comment][5][title]' type='radio' value='hello' />"
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_nested_collections() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1011
def test_nested_fields_with_nested_collections
  form_with(model: @post, scope: "post[]") do |f|
    concat f.text_field(:title)
    concat f.fields("comment[]", model: @comment) { |c|
      concat c.text_field(:name)
    }
  end

  expected = whole_form("/posts/123", method: "patch") do
    "<input name='post[123][title]' type='text' value='Hello World' />" \
    "<input name='post[123][comment][][name]' type='text' value='new comment' />"
  end

  assert_dom_equal expected, output_buffer
end
test_nested_fields_with_new_records_on_a_nested_attributes_collection_association() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 1425
def test_nested_fields_with_new_records_on_a_nested_attributes_collection_association
  @post.comments = [Comment.new, Comment.new]

  form_with(model: @post) do |f|
    concat f.text_field(:title)
    @post.comments.each do |comment|
      concat f.fields(:comments, model: comment) { |cf|
        concat cf.text_field(:name)
      }
    end
  end

  expected = whole_form("/posts/123", method: "patch") do
    '<input name="post[title]" type="text" value="Hello World" />' \
    '<input name="post[comments_attributes][0][name]" type="text" value="new comment" />' \
    '<input name="post[comments_attributes][1][name]" type="text" value="new comment" />'
  end

  assert_dom_equal expected, output_buffer
end
test_submit_with_object_and_nested_lookup() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 928
def test_submit_with_object_and_nested_lookup
  with_locale :submit do
    form_with(model: @post, scope: :another_post) do |f|
      concat f.submit
    end

    expected = whole_form("/posts/123", method: "patch") do
      "<input name='commit' data-disable-with='Update your Post' type='submit' value='Update your Post' />"
    end

    assert_dom_equal expected, output_buffer
  end
end
test_submit_with_object_as_existing_record_and_locale_strings() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 900
def test_submit_with_object_as_existing_record_and_locale_strings
  with_locale :submit do
    form_with(model: @post) do |f|
      concat f.submit
    end

    expected = whole_form("/posts/123", method: "patch") do
      "<input name='commit' data-disable-with='Confirm Post changes' type='submit' value='Confirm Post changes' />"
    end

    assert_dom_equal expected, output_buffer
  end
end
test_submit_with_object_as_new_record_and_locale_strings() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 883
def test_submit_with_object_as_new_record_and_locale_strings
  with_locale :submit do
    @post.persisted = false
    @post.stub(:to_key, nil) do
      form_with(model: @post) do |f|
        concat f.submit
      end

      expected = whole_form("/posts") do
        "<input name='commit' data-disable-with='Create Post' type='submit' value='Create Post' />"
      end

      assert_dom_equal expected, output_buffer
    end
  end
end
test_submit_without_object_and_locale_strings() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 914
def test_submit_without_object_and_locale_strings
  with_locale :submit do
    form_with(scope: :post) do |f|
      concat f.submit class: "extra"
    end

    expected = whole_form do
      "<input name='commit' class='extra' data-disable-with='Save changes' type='submit' value='Save changes' />"
    end

    assert_dom_equal expected, output_buffer
  end
end
url_for(object) click to toggle source
Calls superclass method
# File actionview/test/template/form_helper/form_with_test.rb, line 277
def url_for(object)
  @url_for_options = object

  if object.is_a?(Hash) && object[:use_route].blank? && object[:controller].blank?
    object.merge!(controller: "main", action: "index")
  end

  super
end

Private Instance Methods

form_text(action = "/", id = nil, html_class = nil, local = nil, multipart = nil, method = nil) click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2211
def form_text(action = "/", id = nil, html_class = nil, local = nil, multipart = nil, method = nil)
  txt =  %{<form accept-charset="UTF-8" action="#{action}"}.dup
  txt << %{ enctype="multipart/form-data"} if multipart
  txt << %{ data-remote="true"} unless local
  txt << %{ class="#{html_class}"} if html_class
  txt << %{ id="#{id}"} if id
  method = method.to_s == "get" ? "get" : "post"
  txt << %{ method="#{method}">}
end
hidden_fields(options = {}) click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2195
def hidden_fields(options = {})
  method = options[:method]

  if options.fetch(:skip_enforcing_utf8, false)
    txt = "".dup
  else
    txt = %{<input name="utf8" type="hidden" value="&#x2713;" />}.dup
  end

  if method && !%w(get post).include?(method.to_s)
    txt << %{<input name="_method" type="hidden" value="#{method}" />}
  end

  txt
end
protect_against_forgery?() click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2229
def protect_against_forgery?
  false
end
whole_form(action = "/", id = nil, html_class = nil, local: false, **options) { |: ""| ... } click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2221
def whole_form(action = "/", id = nil, html_class = nil, local: false, **options)
  contents = block_given? ? yield : ""

  method, multipart = options.values_at(:method, :multipart)

  form_text(action, id, html_class, local, multipart, method) + hidden_fields(options.slice :method, :skip_enforcing_utf8) + contents + "</form>"
end
with_locale(testing_locale = :label) { || ... } click to toggle source
# File actionview/test/template/form_helper/form_with_test.rb, line 2233
def with_locale(testing_locale = :label)
  old_locale, I18n.locale = I18n.locale, testing_locale
  yield
ensure
  I18n.locale = old_locale
end