[rails]current_page?がfalseになる

投稿者:

https://railsdoc.com/page/current_page_q

原因

get以外はcurrent_pageつかえんかった(・o・)

ソースを読んでみると・・

https://github.com/rails/rails/blob/main/actionview/lib/action_view/helpers/url_helper.rb

get以外のときはfalseにナッテマスネ(;・∀・)

      def current_page?(options = nil, check_parameters: false, **options_as_kwargs)
        unless request
          raise "You cannot use helpers that need to determine the current " \
                "page unless your view context provides a Request object " \
                "in a #request method"
        end

        return false unless request.get? || request.head?

共通のviewをGET以外のとこで使っているとこで注意したほうがいいですね(T_T)

こうした

とりあえずコントローラーとアクション比較してやりすごした

モジュールが複数あったりすると注意が必要かも

  <% if controller_name == 'ababa' && action_name == 'test' %>

こっちのがいいかも?

test_pathは自動生成されるrouteのpath
<%= request.path_info == test_path %>

なんでなんや

いろいろ経緯があってのことっぽい

https://github.com/rails/rails/issues/17855

日本語でおk

babu
babu

現場で使える Ruby on Rails 5速習実践ガイド