Getting: “Couldn't find SessionsHelper, expected it to be defined in helpers/sessions_helper.rb”

后端 未结 5 872
予麋鹿
予麋鹿 2021-01-19 07:58

I am getting the following in my log file upon loading /login/.

Started GET \"/login\" for 120.138.93.108 at 2016-01-02 03:06:24 +0000

Act

相关标签:
5条回答
  • 2021-01-19 08:24

    I solved this issue by re-creating helpers/sessions_helper.rb (using touch) and filling it with the exact same contents. Weird, but it worked.

    0 讨论(0)
  • 2021-01-19 08:28

    I had a similar problem:

    Couldn't find AgileTeamsHelper, expected it to be defined in helpers/agile_teams_helper.rb
    

    It turns out I had this defined in my helper:

    module AgileTeamHelper
      def td_color(text)
        if text == "Green"
          "green-background"
        elsif text == "Yellow"
          "yellow-background"
        elsif text == "Red"
          "red-background"
        end
      end
    end
    

    Instead of:

    module AgileTeamsHelper # <-- (Notice the extra s after teams)
      def td_color(text)
        if text == "Green"
          "green-background"
        elsif text == "Yellow"
          "yellow-background"
        elsif text == "Red"
          "red-background"
        end
      end
    end
    

    In this case I was missing the 's' in 'AgileTeamsHelper'. I believe that rails looks for a module with the same as the helper, but camel cased. IE. 'agile_teams_helper'.camelize -> "AgileTeamsHelper"

    0 讨论(0)
  • 2021-01-19 08:31

    I simply followed the age-old advice:

    when in doubt, restart your server

    and that was enough for me. I was absolutely sure the code was alright.

    0 讨论(0)
  • 2021-01-19 08:31

    In my case, I had an :

    include ModuleName

    pointing to a non existing module in a model. This one has been previously removed but I had miss the reference.

    I had the exact same "Couln't find ..." error with every helpers I have in my app so it was pretty difficult to find out where the problem came from.

    0 讨论(0)
  • 2021-01-19 08:40

    TL; DR

    Sometimes it's not the problem of helpers. Delete all the helpers and see what happens next.

    Body

    It seems if the loading (reading the class file, eval the content to define the class/module defined inside) of the controller itself fails with whatever reason, it shows this error.

    In my case, there was a class from a gem which was intended to be used by includeing to the controller, which overrides included method, and did some initialization process. The problem was, this include method raised an exception, which prevented the Controller to get loaded, and I don't know why but resulted in a situation just like as described in this question.

    I found out the root cause by deleting all the helpers and checking what happens next. If error changes and you find it to be happening on the loading of the Controller, then now you can fix it.

    # Example class of what have caused the Helper not found in my case
    class FooController
      include SomeGemWhichRaiseExceptionOnInclude
    end
    
    0 讨论(0)
提交回复
热议问题