查询在视图中调用实例变量

时间:2015-08-19 16:44:49

标签: ruby-on-rails

我在我的application.html.erb视图布局中调用了以下内容

<%= @time %>

这就是我的应用程序控制器中的内容:

def time_now
@time = Time.current
end

然而,我的浏览器上没有显示时间。有帮助吗?

由于

3 个答案:

答案 0 :(得分:1)

您确定在{,1}}方法的某个地方调用方法char** p = astring; while (*p != NULL){ printf("%s\n", *p); p++; } 吗?

例如,在您的应用程序控制器中尝试以下操作(并确保没有人在子类中重写此方法:

time_now

因此,如果您的indexdef index @time = Time.current end ,请确保config/routes.rb没有root 'welcome#index'方法或正在调用WelcomeController以便index调用1}}方法来设置super

答案 1 :(得分:0)

试试这个:

@time = Time.now

但您也可以在视图中执行此操作,无需控制器:

<%= Time.now %>

答案 2 :(得分:0)

您一定要查看content_for方法。这个工具会对你有所帮助。正如文档所说 - 简单内容可以作为参数传递。

因此,只需将application.html.erb文件放入其中。

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

    <%= yield %>

  </body>
</html>

然后传递视图中的内容。我从index.html.erb文件传递了:

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

这是简化的控制器:

class UsersController < InheritedResources::Base

  def index
    @time = Time.now
  end
end