LoginSignup
3
0

More than 5 years have passed since last update.

jbuilderでwith_indexを使うときの注意

Last updated at Posted at 2018-04-15

初歩的な内容だけど
普段のイメージでjbuilderファイル内でwith_indexを使っても、nilになる


@array.each.with_index(1) do |e, i|
  json.index i # nil
end

jbuilderで使用するときはto_aが必要だった


@array.each.with_index(1).to_a do |e, i|
  json.index i # 1,2,3...
end

同じインスタンス変数をcontrollerで使ったときはto_aは不要だったのになぜ・・・
理由がわかってないので、ご存じの方いたらコメントくれると嬉しいです

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0