2012-03-24 126 views
0

如果它是網站的某個頁面,是否可以呈現模板的某些部分?或者,如果它是某個頁面,是否也可以在application.html.erb佈局中包含特定的javascript?Ruby on Rails中的條件呈現

+2

[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – 2012-03-24 21:31:36

+1

沒什麼特別的,因爲我不知道 – erogol 2012-03-24 21:46:16

回答

3

也許content_for可以幫助你。

例如:

layout.html.erb

... 
<head> 
    <%= yield :scripts %> 
</head> 
... 
<%= yield %> 

view.html.erb

... 
<% content_for :scripts do %> 
    <script>..</script> 
<% end %> 
... 
1

作爲附錄說什麼railscard我通常這樣做:

在佈局中:

<%= stylesheet_link_tag content_for?(:stylesheets) ? yield(:stylesheets) : "application", :debug => Rails.env.development? %> 

然後視圖

<% content_for :stylesheets %> my_view.js <% end %> 

這樣,你只需要設置content_for如果你想改變鏈輪加載的頂層文件一些特別的東西里面。

+0

你應該有一個幫手,它需要任何數量的參數(* args),併爲你做content_for,這樣你就可以在你的視圖中編寫:<%stylesheets「styleshee1」,「stylesheet2」%>'。 – Robin 2012-03-25 00:10:08