ruby on rails - assets in assets/javascripts loading, but JS inaccessible from static pages -
thank taking @ question.
i working on simple rails 3 app, learn framework, , have trouble understanding asset pipeline.
i have file called map.js in app/assets/javascripts, file contains simple google maps initialize function.
i using require_tree (ie have //= require_tree .
in application.js file), , when visit http://localhost:3000/assets/application.js
see map.js file has been pulled application.js
i have following application.html.erb file:
<!doctype html> <html> <head> <title><%= full_title(yield(:title)) %></title> <%= stylesheet_link_tag "application", media: "all" %> <%= javascript_include_tag "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true" %> <%= csrf_meta_tags %> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <%= render 'layouts/shim' %> <script> google.maps.event.adddomlistener(window, 'load', initialize); </script> </head> <body> <%= render 'layouts/header' %> <div class="container"> <% flash.each |key, value| %> <div class="alert alert-<%= key %>"><%= value %></div> <% end %> <%= yield %> <%= render 'layouts/footer' %> </div> </body> </html>
even though map.js being loaded application.js file, calling initialize function yields reference error (initialize not defined). how access initialize function (which loaded application.js) inline script tag. if include javascript manually using javascript_include_tag
ie <%= javascript_include_tag "map.js" %>
, map renders , works properly, worried js file being loaded twice.
i have 2 questions. why can not access initialize function inline script tag in application.html.erb file though being loaded? there better way remedy situation manual include?
thank help.
try this
<script> window.onload = function () { google.maps.event.adddomlistener(window, 'load', initialize); } </script>
add application.html.erb:
<%= javascript_include_tag "application" %>
also js files have on assets/javascripts folder
and application.js should have this:
// manifest file that'll compiled application.js, include files // // javascript/coffee file within directory, lib/assets/javascripts, vendor/assets/javascripts, // or vendor/assets/javascripts of plugins, if any, can referenced here using relative path. // // it's not advisable add code directly here, if do, it'll appear @ bottom of // compiled file. // // warning: first blank line marks end of what's processed, blank line should // go after requires below. // //= require_tree .
Comments
Post a Comment