【发布时间】:2013-03-31 20:28:36
【问题描述】:
我在视图文件 (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
【问题讨论】:
-
你需要使用 AJAX 来处理这种情况
-
如果用户不编辑,为什么还需要显示日期。可能它很容易在控制器操作中创建日期?
-
@deepak,在编辑问题中的格式时,我注意到您使用制表符进行缩进。标准的 ruby 格式是为每个缩进使用 2 个空格:您可以在 IDE 的设置中更改此设置
标签: ruby-on-rails ruby ajax model-view-controller dynamic