I had written a small web app that gets the date(or any data) from a
external device and print it on screen(browser screen) continously
without refreshing the page.
I am using below form_tag inside view file
(myapp/app/views/index.html.erb):
<%= form_tag(channel_get_dates_path(@channel, :write => 1), :remote =>
true) do %>
<input type="submit" value="Get Today''s
Date">
<input id="get_date" type="text"
value="<%=@write_date%>">
<% end %>
and javascript method in view file (myapp/app/views/create.js.erb):
$("#get_date").val("<%= @get_date %>")
Here, when i press the button it will call the method in controller and
display the date in text field without any page refresh.
My question, Is it possible to make the date to display in text field
continuously by pressing the button only once? I tried calling the
Controller method inside a loop and ended with Double render error. is
it correct way or to make changes in view file?
my controller file(maapp/app/controller/get_dates_controller.rb):
class GetDatesController < ApplicationController
include DateUtilities
before_filter :require_user, :set_channels_menu
def index
@channel = current_user.channels.find(params[:channel_id])
@write_date = @channel.get_dates.write_dates.first
end
def destroy
current_user.get_dates.find_by_get_date(params[:id]).try(:destroy)
redirect_to :back
end
def create
@channel current_user.channels.find(params[:channel_id])
@get_date = @channel.get_dates.write_dates.first
if (@get_date.nil?)
@get_date = GetDate.new
@get_date.channel_id = @channel.id
@get_date.user_id = current_user.id
@get_date.write_flag = params[:write]
end
@get_date.get_date = generate_get_date
respond_to do |format|
@get_date.save
format.html { redirect_to
channel_get_dates_path(@channel) }
format.js
end
end
end
and i have date generation file(myapp/app/lib/date_utilities) as:
module DateUtilities
def generate_get_date
require ''rubygems''
require ''socket''
hostname = ''127.0.0.1''
port = 2000
streamSock = TCPSocket.new( hostname, port )
streamSock.write "ON"
while line = streamSock.gets
k = line
end
k
end
end
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Deepak A. wrote in post #1105859:> My question, Is it possible to make the date to display in text field > continuously by pressing the button only once? I tried calling the > Controller method inside a loop and ended with Double render error. is > it correct way or to make changes in view file?You need a bit of JavaScript to accomplish what you want. Read up on setInterval() function: http://www.w3schools.com/jsref/met_win_setinterval.asp Then use JQuery to update your input field in the function called each time the setInterval() fires: $(''#get_date'').val(current_date); How you get "current_date" is up to you. You can get the current date directly in JavaScript or get it from the server using AJAX (Jquery). http://api.jquery.com/jQuery.get/ -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Robert Walker wrote in post #1105861:> Deepak A. wrote in post #1105859: >> My question, Is it possible to make the date to display in text field >> continuously by pressing the button only once? I tried calling the >> Controller method inside a loop and ended with Double render error. is >> it correct way or to make changes in view file? > > You need a bit of JavaScript to accomplish what you want. Read up on > setInterval() function: > > http://www.w3schools.com/jsref/met_win_setinterval.asp > > Then use JQuery to update your input field in the function called each > time the setInterval() fires: > > $(''#get_date'').val(current_date); > > How you get "current_date" is up to you. You can get the current date > directly in JavaScript or get it from the server using AJAX (Jquery). > > http://api.jquery.com/jQuery.get/thanks for your precious suggestions, i have used setinterval function in view file. And now i able to get date from the device continuously without page refresh. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.