I have ben struggling the past two days getting a this tiny piece of
functionality to work without any success
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