formtastic / formtastic

A Rails form builder plugin with semantically rich and accessible markup.
MIT License
5.21k stars 630 forks source link

Country input crashes with country_select >= 9.0.0 #1381

Open padde opened 7 months ago

padde commented 7 months ago

When using with country_select >= 9.0.0 we get the following error:

ActionView::Template::Error
wrong number of arguments (given 4, expected 1..3)

This is due to the method signature of country_select form builder having changed via https://github.com/countries/country_select/commit/021845dafffb65bb51bb6ebc0e40c540a9e83b1a

I could work on a fix for this however I would like to discuss first, should both the old and the new version be supported, or should an error be raised for versions <= 9.0.0, effectively forcing an upgrade?

padde commented 7 months ago

PS: For anyone else having this problem - as a temporary workaround, we have added the following custom input which uses the new method signature:

module Formtastic
  module Inputs
    class CustomCountryInput
      include Base

      CountrySelectPluginMissing = Class.new(StandardError)

      def to_html
        raise CountrySelectPluginMissing, 'To use the :country input, please install a country_select plugin, like this one: https://github.com/stefanpenner/country_select' unless builder.respond_to?(:country_select)

        input_wrapping do
          label_html <<
            builder.country_select(method, input_options.reverse_merge(priority_countries:), input_html_options)
        end
      end

      def priority_countries
        options[:priority_countries] || builder.priority_countries
      end
    end
  end
end

then used it as

f.input :country, as: :custom_country, ...
AJFaraday commented 7 months ago

@padde Thank you for documenting this issue and the workaround!

HamptonMakes commented 1 month ago

Just ran into this myself.