Recently there was a bug reported to rails which results in an infinite loop when passing a form builder object into a partial.
<%= form_for [:admin, @author], html: { class: 'form form-horizontal', multipart: true, style: 'margin-bottom: 0' } do |f| %>
<%= render partial: 'my_partial', locals: { f: f } %>
<% end %>
This would result in:
Completed 500 in 1701ms (ActiveRecord: 1314.9ms | Allocations: 163362)
SystemStackError (stack level too deep):
app/views/admin/authors/_form.html.erb:357
The bug report, and following comments which acknowledged the bug were helpful, but there was no simple way to reproduce it without building an app to test.
I was able to come up with an executable bug report, which I wanted to share because I think it's not well documented how to test Action View with paths in a single file. This is very useful for investigating bugs and various behaviors in the framework.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails"
end
require "minitest/autorun"
require "action_view"
class BugTest < ActionView::TestCase
def setup
@controller.append_view_path "."
end
def test_stuff
partial = <<~ERB
<%= puts f %>
ERB
File.write("_partial.html.erb", partial)
render inline: <<~ERB
<%= form_with model: false, url: false do |f| %>
<%= render(partial: 'partial', locals: { f: f }) %>
<% end %>
ERB
end
end
However, the script could not reproduce the infinite loop bug.
As one person pointed out, the issue was actually not Rails, but a third-party gem meta_request
and removing it would resolve the issue.
This is still a really helpful script, and I want to re-use it so I'm sharing it here.