Skip to content

I18n lookup breaks when using child_index with non-word characters #1795

Open
@risen

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions