Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
risen opened this issue Nov 21, 2022 · 0 comments
Open

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

risen opened this issue Nov 21, 2022 · 0 comments

Comments

@risen
Copy link

risen commented Nov 21, 2022

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.

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

No branches or pull requests

1 participant