Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix no _method input in form_for namespaced model #49053

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions actionview/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Fix `form_for` missing the hidden `_method` input for models with a
namespaced route.

*Hartley McGuire*

* Fix `render collection: @records, cache: true` inside `jbuilder` templates

The previous fix that shipped in `7.0.7` assumed template fragments are always strings,
Expand Down
3 changes: 2 additions & 1 deletion actionview/lib/action_view/helpers/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,13 @@ def apply_form_for_options!(object, options) # :nodoc:

as = options[:as]
namespace = options[:namespace]
action = object.respond_to?(:persisted?) && object.persisted? ? :edit : :new
action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, :patch] : [:new, :post]
options[:html] ||= {}
options[:html].reverse_merge!(
class: as ? "#{action}_#{as}" : dom_class(object, action),
id: (as ? [namespace, action, as] : [namespace, dom_id(object, action)]).compact.join("_").presence,
)
options[:method] ||= method
end
private :apply_form_for_options!

Expand Down
20 changes: 20 additions & 0 deletions actionview/test/template/form_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,26 @@ def test_form_for_with_new_record_to_model
assert_dom_equal expected, @rendered
end

def test_form_for_with_nested_persisted_to_model
post_form = RecordForm.new(to_model: @post)

form_for([:admin, post_form]) { }

expected = whole_form("/admin/posts/123", "edit_post_123", "edit_post", method: :patch) { "" }

assert_dom_equal expected, @rendered
end

def test_form_for_with_nested_new_record_to_model
post_form = RecordForm.new(to_model: Post.new)

form_for([:admin, post_form]) { }

expected = whole_form("/admin/posts", "new_post", "new_post", method: :post) { "" }

assert_dom_equal expected, @rendered
end

def test_form_for_with_file_field_generate_multipart
form_for(@post, html: { id: "create-post" }) do |f|
concat f.file_field(:file)
Expand Down