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
Post a Comment