What is the deal with Controller additions and helper methods!?

I have ben struggling the past two days getting a this tiny piece of
functionality to work without any success :frowning:

Trying to add a simple helper method to my views in order to ensure
links are only displayed if the user has the proper rights to access
that functionality.

      def show_link(object, label = nil)
        label ||= auth_labels[:show]
        if can?(:read, object)
          link_to(label, object)
        end
      end

But using link_to from within this context, somehow I don't have
access to the controller variable used internally, fx in url_for.

      def url_for(options = {})
        ...
        when :back
          escape = false
          controller.request.env["HTTP_REFERER"] ||
'javascript:history.back()'
        else
        ...
      end

If I output self in the show_link method, I can see that my current
context of self is ProjectsController, so I guess it makes sense that
I don't have a method controller inside a controller?
I access my helper method from here:

# index.html.erb
<%= show_link project %>

And the setup

if defined? ActionController
  ActionController::Base.class_eval do
    AuthAssistant::ViewHelpers::AuthLink
  end
end

# module AuthAssistant::ViewHelpers::AuthLink

      def self.included(base)

base.helper_method :create_link, :delete_link, :edit_link, :show_link

base.helper_method :new_link, :destroy_link, :update_link, :read_link
      end

But using link_to from within this context, somehow I don't have
access to the controller variable used internally, fx in url_for.

That's because the controller itself has its own implementation of
url_for, if you attempt to reuse those helpers in the controller it's
just not going to work. Given that link_to is defined in the same
module as the view's url_for, you probably won't be able to get this
working without serious hacks or code duplication. But I'd question
what you're trying to do here, why are your controllers generating
html without using views?

A ‚Äúsometimes‚ÄĚ use-case is putting a link using link_to in the flash.

I’d recommend render_to_string :inline for this case.

Yehuda Katz
Architect | Engine Yard
(ph) 718.877.1325

All I actually wanted to do was to add some helper methods for my
views.
I kind of "solved" the problem by rethinking my approach, as I could
see I had gone down a "blind alley".

module ApplicationHelper
  def self.auth_assist_helpers
    include AuthAssistant::ViewHelpers
  end
end

module AuthAssistant
  module ViewHelpers
    include AuthAssistant::ViewHelpers::AuthLink
    include AuthAssistant::ViewHelpers::RestLink
    include AuthAssistant::ViewHelpers::AuthMenuItem
  end
end