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?
You can, sort of.
First of all, if you are thinking about a javascript file included like this :
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 :
you would see something like :
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 :
jsfile.js.erb
Include the javascript file like this :
And add the corresponding route in
routes.rb