Home>

As the title says, I want to output the array in the yml file to the view with rails.
Specifically, I want to output the contents line by line according to the number of elements in the array.

undefined method `each'for #<String: 0x000000>
Corresponding source code

I'm writing in slim

--hoge (hoge_entities, identity [: foo_codes]). each do | foo |
  --if foo [: bar] .present?
    --foo [: bar] .each do | bar |
      span = bar
I get an error here.
# =>undefined method `each'for #<String: 0x000000>
def foo
  setting_yml [: bar]
end
foo:
  bar: bar:
    --Hello1
    --Hello2
What I tried

Please describe here what you have tried for the problem.
When I wrote the rails part of the corresponding source code, it became undefined method'each'. Try it on the console

pry (main)>foo = Foo.find (1)
# =>id: 1, name: "Foo", created_at: xxx, updated_at: xxx
pry (main)>foo [: bar] .each do | bar |
pry (main) * puts bar
pry (main) * end
# =>Hello1
# =>Hello2


I was able to output, so I do not understand why slim fails.

By the way, with slim

--hoge (hoge_entities, identity [: foo_codes]). each do | foo |
  --if foo [: bar] .present?
    span = foo.bar

If it is, it will be output correctly.

This is a rudimentary question, but I would appreciate it if you could answer it! !!

  • Answer # 1

    This has been resolved. ..
    Specifically, three yml files are output in one slim file, and each of the three yml files
    foo: bar
    The data was included, and two of the yml files were not in the form of an array as described above, so an error occurred.

    So each yml file has no relation to the number of elements

    foo:
      bar: bar:
        --Hello1
        --Hello2
    foo:
      bar: bar:
        --Hello3
    foo:
      bar: bar:
        --Hello4
        --Hello5

    And, by raising it in the form of an array, each error disappeared.

Related articles