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

  • SEARCH
  • Home
  • 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 6922631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:25:53+00:00 2026-05-27T10:25:53+00:00

I have not been able to understand why one should add extension .erb to

  • 0

I have not been able to understand why one should add extension .erb to an ordinary .js file in the app/assets folder? This is how I came to wonder this:

In my app, I want no more than one http request spent for a js file to be included in the body of the HTML page/DOM. This I can do by adding js filenames (with inclusion of lines like) into the application.js manifest file:

    //= require jquery
    //= require directory_name 
    //= require jQuery_plugins/carousel
    //= require jQuery_plugins/word-editor
    //= require jQuery_plugins/abc_something

and so on.

Now I have a long list of javascripts for the entire rails app, and I want the manifest file to output (to include) javascripts in a page-specific way. To manage this, I attempted to add conditions like:

    if params[:controller] == "my_controller" && params[:action] == "index"
      //= require jQuery_plugins/carousel

Also I converted application.js to application.js.erb file to see if that worked. But no! It did not work. Manifest will output everything that comes its way. Looks like a question was posed for a similar problem on SOF, but solutions given in the answers there seem very old-school and rather unclean traditional way.

Where-do-you-put-your-page-specific-javascript-code

So what is, really, the use of converting js into js.erb file? And can I manage page-specific output purely on the basis a “js manifest” file?

  • 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-05-27T10:25:54+00:00Added an answer on May 27, 2026 at 10:25 am

    So, you have .js.erb so you can embed ruby code in your js, which will be executed and output js into the middle of the file. Maybe that answer is too obvious.

    One reason you might want to do this is to use the asset_path helper to embed the path to another one of the application’s assets in a js string literal. I think you can also use url_for or other route helpers to, say, embed the URL to a particular application action in a JS string literal too. But I haven’t tried that, not every usual Rails helper method is available in asset ERBs, only a subset, and they don’t always work quite exactly like the usual helpers.

    What you can not do is what you were originally proposing, embed ruby that is conditional on the current request or current page. This is because assets in the asset pipeline are not compiled (ie, executed) for every request (at least not ordinarily in production), they are instead only compiled once for the life of the application and cached — so at the time they are compiled and cached, there is no ‘current’ request or page or request params.

    In general, .js.erb probably isn’t used too often, it’s there just for consistency and in case you need it. There are probably other odd cases where it would be convenient to have ruby code generating JS code in an asset, even without using url helper methods. So why not make it possible, since the asset pipeline means assets are going through some processing/execution anyway, it’s just kind of natural to let you send em through ERB too if you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried a lot but not been able to understand the steps involved
I have not been able to find any definitive answers to this question: Can
I have a simple problem that I have not been able to find an
I have another weird problem which I have not been able to solve in
I have searched around and have not been able to find a solution for
So this is something that I have not been able to find any documentation
I have no code to really display here, since I have not been able
Following the standard instruction for using AES algorithm, I have not been able to
I'm working with a Ruby project for school, and have sadly not been able
I have been using this as a reference, but not able to accomplish exactly

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.