Include JavaScript in Ruby (on Rails)

牧云@^-^@ 提交于 2019-12-25 02:41:55

问题


I don't quite understand how Rails includes JavaScript files. Google and the following documentations didn't solve my problem:
http://railsapps.github.io/rails-javascript-include-external.html
http://guides.rubyonrails.org/asset_pipeline.html

I understand that Rails uses manifest-files (e.g. applications.js) to pack every included JavaScript file into one big file (performance reasons, etc.).

In application.html.erb you need to include this manifest, like

<%= javascript_include_tag :application %>

I added a JavaScript file to /app/assets/ (e.g. my_javascript.js).
So, if I look at the Source-Code, I can find

<script src="/assets/application.js" type="text/javascript"></script>

and if I open the file, the content of my_javascript.js is rendered in the application.js file.

My Problem: The function is not working. But if I directly include the my_javascript.js in the application.html.erb, like

<%= javascript_include_tag "my_javascript" %>

it is working! Of course, it renders a second script-tag, like:

<script src="/assets/my_javascript.js" type="text/javascript"></script>

So finally, why does the function work if I include it in specific and not if I use the manifest (how it's supposed to)? Do I need to consider something special about the Assets-Pipeline? My manifest includes

//= require_tree .

but I even tried it with

//= require my_javascript

I'm on Rails 3.2.3 with Ruby 1.9.3.

Thank you in advance!


回答1:


Try putting in Application Layout

<%= yield :head %> in the between head tags.



来源:https://stackoverflow.com/questions/18592464/include-javascript-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!