javascript - 在 Rails 中添加 Javascript

标签 javascript html ruby-on-rails-3

我正在使用 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/

相关文章:

javascript - Gmaps.js 未在 Bootstrap 模式内加载

sql - Ruby on Rails - SQL 查询计数

ruby-on-rails - 在代码中使用 sed

javascript - 将一个对象数组中的属性值映射到另一个对象数组 Javascript 中的属性

javascript - 如何在客户端使用 SheetJS 将数据导出到 TSV 文件?

javascript - 在我们滚动覆盖导航页面时防止主页滚动

ruby-on-rails - 从Ruby on Rails中的模型和 View 进行逆向工程(生成)表或数据库模式

javascript - 移动菜单打开时 body 的滚动锁定不起作用

javascript - 无法获取 html5 地理位置坐标来工作

html - 为什么在某些html页面中CSS部分被注释了?