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 6132151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:03:57+00:00 2026-05-23T17:03:57+00:00

I have a model with lots of parameters and a function giving all the

  • 0

I have a model with lots of parameters and a function giving all the parameter names. Now I’d like to have a global Javascript file using this function like this

function bar() {
    <% Model.parameter_names.each do |name|
       parameters.push("<%= name %>");
    <% end %>
    // do something with them
}

The *parameter_names* method is a Class Method (*def self.parameter_names*), so i do not need an object.
Is this possible?

  • 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-23T17:03:58+00:00Added an answer on May 23, 2026 at 5:03 pm

    You can, sort of.

    First of all, if you are thinking about a javascript file included like this :

    <script src="your-js-file.js"></script>
    

    You can forget about it. In no way, RoR is doing anything for that file.

    You can mix javascript with RoR in controller responses because that js file is read by RoR, then evaluated for any Ruby expressions, then returned as the response to your browser. In fact, if you take a look at the js response from the controller for something like you wrote :

    function bar() {
        <% Model.parameter_names.each do |name|
           parameters.push("<%= name %>");
        <% end %>
        // do something with them
    }
    

    you would see something like :

    function bar() {
       parameters.push("abc");
       parameters.push("def");
       parameters.push("ghi");
       parameters.push("jkl");
    }
    

    Javascript is a client side technology. The only way to add server side code in it is to actually generate the js file with server side code. This is probably your best shot at it.

    Controller :

    def jsfile
        @variable = "hello, world!"
        respond_to do |format|
          format.js
        end
    end
    

    jsfile.js.erb

    function bar() {
      alert('<%= @variable %>');
    }
    

    Include the javascript file like this :

    <script src="<%= jsfile_controllername_path %>"></script>
    

    And add the corresponding route in routes.rb

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

Sidebar

Related Questions

I have a model with lots of say CharField fields, that I would like
I have a fairly large EF4 model, using POCO code gen. I've got lots
If I have Model.objects.all() I want to get only one object for any content_object=foo,
I have some model objects I'm using in my Java client application. Later these
I have a model, Thing, that has a has_many with ThingPhoto, using Paperclip to
I have a function (neural network model) which produces figures. I wish to test
I have lots of Activities chained together with Intents and some Intents require parameters
I have a model with many different children (has_many). My app needs to lots
I have such model now: receipt contains components. component contain product. The difference between
Suppose you have an application that utilizes the domain-model pattern, DDD and lots of

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.