LoginSignup
2
0

More than 3 years have passed since last update.

railsアプリで simple calendar を使ったページにて、undefined local variable or method `block' for

Posted at

環境

ruby 2.5.1
rails 5.2.4

gem 'simple_calendar', '~> 2.0'

背景

  1. Docker+ CircleCiを導入し、ローカルでCircleCIによるrspecが正しく機能するかテスト
  2. 以前にはなかった箇所でrspecがfalseになる
  3. simple-calendarを使ったページにて、エラーが発生していた
ActionView::Template::Error - undefined local variable or method `block' for #<#<Class:0x00007ffddca4a6a0>:0x00007ffddc568d58>:
  app/views/simple_calendar/_month_calendar.html.erb:25:in `view template'
  app/views/application/_calendar.html.erb:1:in `_app_views_application__calendar_html_erb___1293910406710393037_70364149977560'
  app/views/events/index.html.erb:4:in `_app_views_events_index_html_erb___316186926983959569_70364150058740'

_month_calendar.html.erb
    <% week.each do |day| %>
            <%= content_tag :td, class: calendar.td_classes_for(day) do %>
              <% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(block) %>
                <% capture_haml(day, sorted_events.fetch(day, []), &block) %>
              <% else %>
                <% block.call day, sorted_events.fetch(day, []) %> #この行にある"block"が定義されていないらしい
              <% end %>
            <% end %>
          <% end %>
        </tr>
      <% end %>

試したこと

1.以下のsimle calenderのviewを生成するコマンドで初期のhtmlを作成

rails g simple_calendar:views

2.カスタマイズしていたhtmlを初期のhtmlに置き換える
3.正常にカレンダーが表示された

結論

HTMLの記述が違っていた。
"block" ではなく、 "passed_block" にしなければならない

_month_calendar.html
#エラーが出たhtml
<% block.call day, sorted_events.fetch(day, []) %>


# 正常に動作するhtml
<% passed_block.call day, sorted_events.fetch(day, []) %>

"passed_"の部分を間違って消してしまったのか???
githubで過去に正常に動いていた時のバージョンを調べても、"block" と記載となっていたのだが、、、
simple calendar側で変更があったのだろうか?

simple calendar 側で view の一部に変更があった

simple calendarのgithubを調べたところ、最新の変更にて"block"から"passed_block" になっていた。
simple calendarのgithub

app/views/simple_calendar/_month_calendar.html.erb
        <tr>
          <% week.each do |day| %>
            <%= content_tag :td, class: calendar.td_classes_for(day) do %>
 -            <% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(block) %>
 -              <% capture_haml(day, sorted_events.fetch(day, []), &block) %>
 +             <% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(passed_block) %>
 +              <% capture_haml(day, sorted_events.fetch(day, []), &passed_block) %>
              <% else %>
 -              <% block.call day, sorted_events.fetch(day, []) %> # 削除された
 +              <% passed_block.call day, sorted_events.fetch(day, []) %> # 追加された
              <% end %>
            <% end %>
          <% end %>

こういったgem側の変更によって、自分のアプリに影響が及ぶケースは, "impressionist"と言うgemでのエラーで経験していたので、
そんなに詰まることなく、原因究明できました。

2
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
2
0