views:

29

answers:

1

I wand to change the regular expression in my application.js file from within a rails controller.
Is that possible?

+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
I dont really understand your solution how can I use a erb rendered file in my application.js file?
Markus
Example added above.
Jakub Hampl
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
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
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...
Markus
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