2012-10-25 25 views
1

我正在使用Rails 3.0。我想添加一个Javascript文件show_javascript.js/public/javascripts/中查看show.html.erb在Rails中添加Javascript

show.html.erb文件从模板application.html.erb

采取<head>...</head>一部分,我不知道我应该如何添加它。

回答

1

假设你有一个通用的样式布局,你可以添加以下application.html.erb

<%= javascript_link_tag 'show' if params[:action] == 'show' %> 

你甚至可以使用content_for PARAMS加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 -%> 
+0

我觉得@ Nathan给出了最完美的答案,如果它仅用于show.html.erb,那么使用content_for params加上yield是要走的路。 (只是,内森必须改变他的css标签为js标签:D) – sameera207

+0

@ sameera207 * fixed *谢谢,哈哈,不知道我在想什么:P –

+0

是不是'''javascript_include_tag'''? http://apidock.com/rails/v4.1.8/ActionView/Helpers/AssetTagHelper/javascript_include_tag –