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 [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to