rails: how do i access a method in my application controller?

前端 未结 4 1786
猫巷女王i
猫巷女王i 2021-02-06 01:09

Noob scoping issue, I imagine. :\\

class ApplicationController < ActionController::Base
  protect_from_forgery

  @locations = get_locations

  def get_locat         


        
4条回答
  •  余生分开走
    2021-02-06 01:50

    I imagine that this line:

    @locations = get_locations
    

    ... is trying to access the class level method get_locations and not the instance method.

    The clue here is that the error message is showing that it can't find it on the class itself (ApplicationController:Class) and not an instance of that class. That means that you're in the class scope, not instance scope.

    This would fix it:

      def self.get_locations
         Location.where(:active => true).order('name').all
      end
    

提交回复
热议问题