0

I have an application that needs to display a clock on the index page. So I have a static controller like this:

controllers/static_controller.rb:

class StaticController < ApplicationController

  def index
    @time = Time.now.strftime("%H:%M:%S ")
  end

  def get_time
    @time = Time.now.strftime("%H:%M:%S ")
    render partial: "date"
  end

end

and I have this on views/static/index.html.erb:

<div class="time-container">
   <%= render partial: "date" %>
</div>

and this partial views/static/_date.html.erb that contains just the var:

  <%=@time%>

I used this JS function to try to update time on assets/javascripts/static.js:

$(document).ready(function () {
    setInterval(function () {
        $('.time-container').load('/static/get_time');

    }, 1000);
});

My routes.rb are this:

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root "static#index"
  resources :static

end

This way, the clock shows up when the page loads, but it stays always the same and does not update. Am I missing something?

Luiz Henrique
  • 877
  • 8
  • 25

1 Answers1

1

Your routes don't match with the flow you built.

You just were lucky that static/index worked because its included within the resources statement.

Take out the:

resources :static

And replace with

get 'static/index'
get 'static/get_time'
Hernan Velasquez
  • 2,770
  • 14
  • 21