Topic: split is undefined in javascript

trying to use split function from javascript in my rails application


s.split is not a function is what I am getting.

Any ideas what i am doing ?


Re: split is undefined in javascript

You're mixing Ruby and JavaScript.

If you want to mix Ruby and JavaScript, it would look more like this:


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:


def alert
  @s = Foobar.find(....)

so 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)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.