0

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

1 Answer 1

1

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 %>);
Sign up to request clarification or add additional context in comments.

6 Comments

I dont really understand your solution how can I use a erb rendered file in my application.js file?
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?
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}/);.
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}/?
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...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.