2012-10-25 26 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 –