simple_form icon indicating copy to clipboard operation
simple_form copied to clipboard

I18n lookup breaks when using child_index with non-word characters

Open risen opened this issue 2 years ago • 0 comments

Environment

  • Ruby 2.7.6
  • Rails 6.1.7
  • Simple Form 5.1.0

Current behavior

In form_builder.rb there's the following code:

    def lookup_model_names #:nodoc:
      @lookup_model_names ||= begin
        child_index = options[:child_index]
        names = object_name.to_s.scan(/(?!\d)\w+/).flatten
        names.delete(child_index) if child_index
        names.each { |name| name.gsub!('_attributes', '') }
        names.freeze
      end
    end

I was using "#index" as the child_index option, to distinguish it from normal names (I'm doing some javascript fiddling to replace the "#index" with something proper later on).

I noticed the hints weren't being properly translated. Looking through the old issues I found #245 and #246, which looked similar to my issue. I realized it was because the regex /(?!\d)\w+/ would only match word-characters, so it didn't match the #-character.

I monkey-patched the regex to /(?!\d)[\w#]+/ or even /(?!\d)[^\[\]]+/ which fixed my issue.

Not sure if SimpleForm should be more tolerant of special characters in child_index or not, but I just wanted to raise it in case other people encountered this problem.

risen avatar Nov 21 '22 11:11 risen