Search code examples
javascriptruby-on-rails-3unobtrusive-javascript

Unobtrusive way to do $(document).ready(.....) in rails 3


In one of my views I need to do something upon loading of the page (load google maps JS and then initialize some of my stuff) I can certainly embed this bit into my haml/erb template but is there an UJS way to do this properly?

-content_for :head do
  = google_loader_tag  

#map_canvas

:javascript
  $(document).ready(function() {
    google.load('maps', '3', {other_params:'sensor=false', callback: initialize});
  });

I did think that UJS meant not mixing Javascript and html templates together, but here I see no other option. What do you think?


Solution

  • as Confusion said in comments - it is UJS way

    i would add that it's good practice to have such stuff inside content_for :document_ready and then yield :document_ready at the bottom of your <body> tag in your application layout