我在视图文件(index.html.erb)中使用下面的 button_to 标记
<textarea rows="2" cols="20"><%=@write_date%></textarea>
<%= button_to t(:get_date_write_new),channel_get_dates_path(@channel, :write => 1) %>
当我单击按钮时,它将从设备获取日期并将其打印在屏幕上。
我的问题是我如何连续调用函数“channel_get_dates_path(@channel, :write => 1)”,并在 textarea 中显示日期而不刷新页面。只按一次按钮?
我的控制器文件:
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 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
@get_date.save
redirect_to channel_get_dates_path(@channel)
end
end
我有日期生成文件(myapp/lib)为:
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