I wand to change the regular expression in my application.js file from within a rails controller.
Is that possible?
views:
29answers:
1
+1
A:
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 %>);
Jakub Hampl
2010-05-12 12:32:29
I dont really understand your solution how can I use a erb rendered file in my application.js file?
Markus
2010-05-12 12:56:15
Example added above.
Jakub Hampl
2010-05-12 13:01:15
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?
Markus
2010-05-12 13:10:42
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}/);`.
Jakub Hampl
2010-05-12 13:16:23
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
2010-05-12 13:33:16
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...
Markus
2010-05-12 13:35:28
Did you actually try out the code I posted? Some thing are easier seen then said. Your http://localhost:3000/application.js file will be what is in `app/views/my_js_controller/show.js.erb`.
Jakub Hampl
2010-05-12 14:41:14