我使用的内部视图文件(index.html.erb)button_to标签下面Ruby on Rails的更新页面,AJAX
<textarea rows="2" cols="20"><%[email protected]_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
你需要为这种情况使用AJAX然后 – swapnesh 2013-04-09 12:08:09
为什么你需要显示日期,如果用户不会编辑它。可能它很容易在控制器操作中创建日期? – itsnikolay 2013-04-09 12:39:44
@deepak,同时在你的问题中编辑格式,我注意到你正在使用制表符缩进。标准的ruby格式是为每个缩进使用2个空格:你可以在你的IDE的设置中改变这个 – 2013-04-09 12:52:47