我打電話我application.html.erb視圖佈局如下調用實例變量查詢有關鑑於
<%= @time %>
這是什麼在我的應用程序控制器:
def time_now
@time = Time.current
end
然而,時間不顯示在我的瀏覽器上。任何幫助?
感謝
我打電話我application.html.erb視圖佈局如下調用實例變量查詢有關鑑於
<%= @time %>
這是什麼在我的應用程序控制器:
def time_now
@time = Time.current
end
然而,時間不顯示在我的瀏覽器上。任何幫助?
感謝
你確定你所呼叫的方法time_now
某處你的,比方說,index
方法我從index.html.erb
文件傳遞?
例如,嘗試在你的應用程序控制器以下(並確保沒有人在子類中重寫此方法:
def index
@time = Time.current
end
因此,如果您config/routes.rb
有root 'welcome#index'
,確保WelcomeController
不具有index
方法或正在調用super
以便調用ApplicationController#index
方法來設置@time
。
試試這個:
@time = Time.now
但是你也可以做到這一點在你看來,不控制:
<%= Time.now %>
你一定要考慮的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
'current()'是添加到類「Time」的Rails特定方法。 – nimrodm