Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8247221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:46:17+00:00 2026-06-07T22:46:17+00:00

In my Rails app, I need scripts to behave differently depending on which controller

  • 0

In my Rails app, I need scripts to behave differently depending on which controller and action they are served from. So in my ApplicationController, I make these available like so:

before_filter :save_action_controller  

def save_action_controller
  @action = action_name
  @controller = controller_name
end

And in an erb-filtered javascript file I have this:

window.controller = <%= @controller || 'undefined' %>;
window.action = <%= @action || 'undefined' %>;

But it seems that @controller and @action are both nil in this context? However, I can access them from views and helpers. Also, I don’t see how this can go by without raising an exception, if I were really trying to access ‘non-existent’ variables?

What do I need to do to access this from javascript? Is there a preferred method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:46:18+00:00Added an answer on June 7, 2026 at 10:46 pm

    This railscast episode has some info on passing data to javascript.

    There are several ways to do it but one nice approach is to use data attributes (as shown in railscast). You can define it with html like so:

    <h1>Products</h1>
    
    <div id="products" data-url="<%= products_url %>">
      Loading products...
    </div>
    

    and then access it via javascript

    jQuery ->
      alert $('#products').data('url')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have the rails app, content served from the database. Now we need to
So I need to access this service from my rails app. I'm using soap4r
I need to produce somewhat complicated PDFs from a Rails app - tables with
I need to start a copy of a Rails app from within Java. I
I need a 6 character alphanumeric ID for use in my rails app, which
I am building a small rails app and I need a way to generate
I need some references for creating a Rails app where each client/company has his
I have a Rails app that does everything I need it to do via
I have a Rails app running Mongoid on Heroku and I need to set
I need to implement fine-grained access control in a Ruby on Rails app. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.