I wand to change the regular expression in my application.js file from within a rails controller.
Is that possible?
1 Answer
Yes it is possible. You can use your Routes file to define it as a an action and then just use ERB to render it. Though it's much better if you can avoid it.
routes.rb
map.myjs '/application.js', :controller => 'my_js_controller', :action => :show
my_js_controller.rb
def show
end
my_js_controller/show.js.erb
$('awesome jquery code').match(<%= render_my_regex_here %>);
6 Comments
Markus
I dont really understand your solution how can I use a erb rendered file in my application.js file?
Markus
Thanks for your example, but I still don't see trough. how can I reference the render_my_regex_here in the application.js file?
Jakub Hampl
If you do this and say
render_my_regex_here evaluates to /[a-z]{3,5}/ then when you access /appliaction.js it will be $('awesome jquery code').match(/[a-z]{3,5}/);.Markus
if I get you right, I can just ad $('awesome jquery code') in the application.js file and that would be /[a-z]{3,5}/?
Markus
I've the following code in my application.js: if(reg_exp_url.test(document.location.href))... so all I want is to put that /[a-z]{3,5}/ espression in to the reg_exp_url variable...
|