I just encounter a problem similar to http://www.ruby-forum.com/topic/216433
I need to access controller instance variable from helper.
Here is the code I have done, and it’s not work.
Controller:
class ApplicationController < ActionController::Base
helper_method :set_background_type #for action only background setting
#for controller-wise background setting
def set_background_type(string)
@background_type = string
end
end
Helper:
module ApplicationHelper
def background_type
@background_type || 'default'
end
end
Layout:
<body class="<%= background_type %>">
After some searching. I got one solution from
http://api.rubyonrails.org/classes/ActionController/Helpers/ClassMethods.html
and the Controller becomes:
and remove the method from ApplicationHelper.
It’s work for this scenario, but I’m still curious,
is there a way to access controller instance variable for method in ApplicationHelper?