我正在使用 Rails 3.0。我想添加一个Javascript文件show_javascript.js
位于 /public/javascripts/
查看 show.html.erb
。
show.html.erb
文件采用 <head>...</head>
模板的一部分 application.html.erb
我想知道应该如何添加它。
最佳答案
假设您有一个通用样式表布局,您可以将以下内容添加到 application.html.erb
:
<%= javascript_link_tag 'show' if params[:action] == 'show' %>
您甚至可以使用 content_for
参数加上 yield
在 <head>
像这样的部分:
layout.html.erb
<head>
<%= yield(:header) if @content_for_header %>
</head>
product/show.html.erb
<% content_for :header do -%>
<%= javascript_link_tag 'show_product' %>
<% end -%>
关于javascript - 在 Rails 中添加 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13061047/