I am currently facing an issue in my Rails app where I need scripts to have different behaviors based on the controller and action they originate from. To address this, I have set up the following code in my ApplicationController:
before_filter :save_action_controller
def save_action_controller
@action = action_name
@controller = controller_name
Furthermore, in a javascript file filtered by erb, I have included the following snippet:
window.controller = <%= @controller || 'undefined' %>;
window.action = <%= @action || 'undefined' %>;
The issue I am encountering is that @controller and @action appear to be null within this context. Strangely, I can access them from views and helpers without any problems. Additionally, I am puzzled by how this doesn't trigger an exception when attempting to retrieve variables that don't exist.
What steps should I take to successfully access this data from javascript? Is there a recommended approach for achieving this?