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