2015-08-19 45 views
0

我打電話我application.html.erb視圖佈局如下調用實例變量查詢有關鑑於

<%= @time %> 

這是什麼在我的應用程序控制器:

def time_now 
@time = Time.current 
end 

然而,時間不顯示在我的瀏覽器上。任何幫助?

感謝

回答

1

你確定你所呼叫的方法time_now某處你的,比方說,index方法我從index.html.erb文件傳遞?

例如,嘗試在你的應用程序控制器以下(並確保沒有人在子類中重寫此方法:

def index 
    @time = Time.current 
end 

因此,如果您config/routes.rbroot 'welcome#index',確保WelcomeController不具有index方法或正在調用super以便調用ApplicationController#index方法來設置@time

0

試試這個:

@time = Time.now 

但是你也可以做到這一點在你看來,不控制:

<%= Time.now %> 
+0

'current()'是添加到​​類「Time」的Rails特定方法。 – nimrodm

0

你一定要考慮的content_for方法。這是該工具將幫助你。正如文檔所說 - 簡單的內容可以作爲參數傳遞。

所以,簡單地放在application.html.erb文件裏面。

<!DOCTYPE html> 
<html> 
    <head> 
    <title>SamplAdmin</title> 
    </head> 
    <body> 
    <%= content_for :time %> 

    <%= yield %> 

    </body> 
</html> 

然後從視圖中傳遞內容。

<p id="notice"><%= notice %></p> 
<% content_for :time, @time %> 

這裏是簡化控制器:

class UsersController < InheritedResources::Base 

    def index 
    @time = Time.now 
    end 
end