jquery - How do I call a JavaScript function from an erb template? -


if define javascript function , call erb template function not called. why? clicking link should show alert, not.

= link_to "add sprout", new_sprout_path, remote: true 

controller:

def new   @sprout = sprout.new   respond_to { |format| format.js } end 

then in template call function:

# sprouts/new.js.erb dosomething(); 

the function defined in js files:

# javascripts/sprouts.js.coffee dosomething = ->   alert "yum ghum new sprout." 

the javascript comes back. can see in console. see no alert. why doesn't not work?

i'm using rails 4.

@dosomething = -> alert "yum ghum new sprout." 

or

window.dosomething = -> alert "yum ghum new sprout." # prefer approach, it's more explicit. 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -