Administrate: 0.13.0 regressions with nested forms (and polymorphic relations?)

Created on 14 Mar 2020  Â·  4Comments  Â·  Source: thoughtbot/administrate

  • What were you trying to do?

Upgrading to Administrate 0.13.0.

  • What did you end up with (logs, or, even better, example apps are great!)?

I use administrate 0.12.0 and administrate-field-nested_has_many 1.2.0. With administrate 0.13.0, the gem now throws this error:

     ActionView::Template::Error:
       undefined method `school' for #<ActiveRecord::Associations::CollectionProxy []>
     # ./lib/administrate/field/nested_has_many.rb:23:in `nested_fields'
     # ./lib/administrate/field/nested_has_many.rb:29:in `nested_fields_for_builder'
     # ./app/views/fields/nested_has_many/_fields.html.erb:2:in `___sers_sdubois__epos_administrate_field_nested_has_many_app_views_fields_nested_has_many__fields_html_erb__2516551389881800165_70257731404460'
     # ./app/views/fields/nested_has_many/_form.html.erb:14:in `___sers_sdubois__epos_administrate_field_nested_has_many_app_views_fields_nested_has_many__form_html_erb__1295690092843922213_70257781048500'
     # ./spec/features/has_many_spec.rb:23:in `block (2 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # NoMethodError:
     #   undefined method `school' for #<ActiveRecord::Associations::CollectionProxy []>
     #   ./lib/administrate/field/nested_has_many.rb:23:in `nested_fields'

When loading the school form in the gem's dummy app, the error above is triggered when building a nested Student's school field of type BelongsTo. It can be reproduced by updating the gem's administrate dependency to 0.13.0 and running the tests.

It appears a regression due to rescue NameError which was removed in https://github.com/thoughtbot/administrate/pull/920/files#diff-a4a632998186059ef606368d710ac173L36.

Administrate::Page::Form#attributes calls Administrate::Page::Base#attribute_field then get_attribute_value executes resource.public_send(attribute_name) with resource=Foo::Student::ActiveRecord::Associations::CollectionProxy and attribute_name="school". This throws a NoMethodError, which is a descendent of NameError.

The exception is no longer rescued in Administrate 0.13.0 for reasons explained in https://github.com/thoughtbot/administrate/issues/480. The rescue clause had previously been added in this commit titled "Add support for polymorphic relationships". So potentially there could also be a regression with the handling of polymorphic relations (I can't say as I haven't used them yet)?

I observe issues in my own app when loading the form for Posts which has a NestedHasMany translations association which themselves have a locale field of a type based on CollectionSelect (adapted for my own needs). This is currently blocking me from upgrading to Administrate 0.13.0.

As the problem potentially occurs in different kinds of fields I'm opening the issue on this repo and not e.g. on the NestedHasMany repo.

@pablobm would you have any idea how to ensure forms with nested fields keep working while avoiding silent errors with incorrect association names?

  • What versions are you running?

    • Rails 6.0.2.1

    • administrate 0.13.0

    • administrate-field-nested_has_many 1.2.0

bug fields models

Most helpful comment

@Timmitry - Thank you for your report. I have filed a separate issue at https://github.com/thoughtbot/administrate/issues/1586

All 4 comments

We are having similar problems - updating to 0.13 breaks all of our virtual fields (and we have many of those). Since 0.13 fixes a CVE, this is kind of an urgent problem 😕

We fixed the issue temporarily by adding an initializer to config/initializers with the following code:

# Monkey patch to make the … page work again
# The breaking change was https://github.com/thoughtbot/administrate/commit/dc856a917aa67e998860bb42664b5da94eb0e682#diff-a4a632998186059ef606368d710ac173
# Issue is open at https://github.com/thoughtbot/administrate/issues/1570
raise "Try to remove this monkey patch when updating Administrate" if Gem.loaded_specs["administrate"].version != Gem::Version.new("0.13.0")

module Administrate
  module Page
    class Base
      protected

      def get_attribute_value(resource, attribute_name)
        resource.public_send(attribute_name)
      rescue NameError
        nil
      end
    end
  end
end

@Timmitry - Thank you for your report. I have filed a separate issue at https://github.com/thoughtbot/administrate/issues/1586

Closing this, and splitting the issue into two:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brynmrk picture brynmrk  Â·  3Comments

MatthiasRMS picture MatthiasRMS  Â·  3Comments

trandoanhung1991 picture trandoanhung1991  Â·  3Comments

kwerle picture kwerle  Â·  4Comments

SleeplessByte picture SleeplessByte  Â·  4Comments