christo16
christo16

Reputation: 4843

Rails 3- Plugin returns 'undefined local variable'

I have a custom plugin (I didn't write it) that is not working on rails 3, however it did work with rails 2. It is for a custom authentication scheme, here is what the main module looks like:

#lib/auth.rb
module ActionController

  module Verification
    module ClassMethods
      def verify_identity(options = {})
        class_eval(%(before_filter :validate_identity, :only => options[:only], :except => options[:except]))
      end
    end
  end

  class Base
    #some configuration variables in here

    def validate_identity
      #does stuff to validate the identity
    end
  end

end

#init.rb
require 'auth'
require 'auth_helper'
ActionView::Base.send(:include, AuthHelper)

AuthHelper contains a simple helper method for authenticating, base on a group membership.

When I include 'verify_identity' on an actioncontroller:

class TestController < ApplicationController
  verify_identity
  ....
end

I get a routing error: undefined local variable or method `verify_identity' for TestController:Class. Any ideas how I can fix this? Thanks!

Upvotes: 0

Views: 455

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107728

It worked in 2.3 because there was an ActionController::Verification module back there. It's not working in 3.0 because this module doesn't exist. Rather than relying on Rails to have a module that you can hook into, define your own like this:

require 'active_support/concern'
module Your
  module Mod
    extend ActiveSupport::Concern
    module ClassMethods
      def verify_identity(options = {})
        # code goes here
      end
    end
  end
end

and use:

ActionController::Base.send(:include, Your::Mod)

To make its functions available. ActiveSupport::Concern supports you having a ClassMethods and InstanceMethods module inside your module and it takes care of loading the methods in these modules into the correct areas of whatever the module is included into.

Upvotes: 3

Related Questions