0

I'm calling the following in my application.html.erb view layout

<%= @time %>

And this is what's in my application controller:

def time_now
@time = Time.current
end

Yet the time is not displaying on my browser. Any helps?

Thanks

3 Answers 3

1

Are you sure you are calling the method time_now somewhere in your, say, index method?

E.g., try the following in your application controller (and make sure no one is overriding this method in a subclass:

def index
  @time = Time.current
end

So if your config/routes.rb has a root 'welcome#index', make sure the WelcomeController does not have an index method or is calling super so that the ApplicationController#index method is called to set @time.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

@time = Time.now

But you can also do this in your view, without controller:

<%= Time.now %>

1 Comment

current() is a Rails specific method added to class Time.
0

You should definitely look into the content_for method. This is the tool will help you. As documentation said - simple content can be passed as a parameter.

So, simply put inside application.html.erb file.

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

    <%= yield %>

  </body>
</html>

Then pass the content from the view. I passed from index.html.erb file as :

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

Here is the simplified controller :

class UsersController < InheritedResources::Base

  def index
    @time = Time.now
  end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.