Topic: split is undefined in javascript
trying to use split function from javascript in my rails application
alert(s.split(',')[1]);
s.split is not a function is what I am getting.
Any ideas what i am doing ?
thanks,
You are not logged in. Please login or register.
Rails Forum - Ruby on Rails Help and Discussion Forum » XHTML/CSS/JavaScript » split is undefined in javascript
trying to use split function from javascript in my rails application
alert(s.split(',')[1]);
s.split is not a function is what I am getting.
Any ideas what i am doing ?
thanks,
You're mixing Ruby and JavaScript.
If you want to mix Ruby and JavaScript, it would look more like this:
app/views/foobar/alert.js.erb:
alert("<%= @s.split(',')[1] %>")This is a JavaScript file that is pre-processed with embedded ruby. Embedded ruby will pre-process the file and call split on the @s instance variable. You have to make sure the @s variable is loaded up, probably in the controller, so maybe like this:
app/controllers/foobar_controler.rb:
def alert
@s = Foobar.find(....)
endso then, in a view, if you did this:
<%= link_to "show alert", "/foobar/alert", :remote=>true %>Whatever was returned into the instance variable @s, will be split on the comma, and the second item will show in the alert.
WARNING, highly simplified, there may be missing pieces, just trying to illustrate the basic point, read more to get it working 100%
Last edited by BradHodges (2011-12-06 02:38:24)
Hosting provided by aTech Media