【问题标题】:ruby on rails update page with ajaxruby on rails 使用 ajax 更新页面
【发布时间】: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


【解决方案1】:

要在不刷新页面的情况下更改页面,您需要 AJAX。

这可以通过在表单中​​传递:remote =&gt; true 属性在rails 中轻松完成:

<%= form_tag(myupdate_path, :remote => true) do %>
  <%= form_tag(channel_get_dates_path(@channel, :write => 1), :remote => true) do %>
     <input type="submit" value="Get Today's Date">
     <input id="thedate" type="text" value="<%=@write_date%>">
  <% end %>
<% end %>

远程选项将调用控制器中的myupdate 方法,并告诉它呈现一个javascript 视图而不是html 视图。假设您的控制器中有:

def myupdate
  @thedate = params['wathever'] # get the params from the form
end

然后您可以在 myController/myupdate.js.erb 中添加您的 javascript 方法(而不是 myupdate.html.erb)。假设您使用的是 jquery,这将是

$("#thedate").val("<%= @thedate %>") # dynamically update

通过这样做,您可以从表单中获取参数,使用控制器根据需要处理它,并告诉 javascript 根据您刚刚放入实例变量的内容更新您的页面。

编辑:我以您的表单为例。希望 javascript 现在更清晰:javascript 视图的想法是获取页面中的“thedate”html 元素,并根据更新的实例变量@thedate 设置其值。这是您告诉 javascript 动态更新您的页面的地方。

希望对你有帮助

【讨论】:

  • 感谢您的宝贵建议,现在我无需刷新页面即可在文本字段中获取日期。
  • 我还有一个问题,每当我按下按钮时,它将调用控制器中的方法并在文本字段中显示日期而无需任何页面刷新,是否可以使日期显示在文本字段中连续按一次按钮?我尝试在循环中调用 Controller 方法并以 Double 渲染错误结束。是正确的方法还是在视图文件中进行更改?
【解决方案2】:

@Aurel,感谢您的建议。我跟着你的步骤。

在我的视图文件中将代码更改为:

  <%= form_tag(channel_get_dates_path(@channel, :write => 1), :remote => true) do %>
     <input type="submit" value="Get Today's Date">
     <input type="text" value="<%=@write_date%>">
  <% end %>

之后,当我按下提交按钮时,会调用更新方法(我可以看到设备正在发送日期),但日期未显示在文本字段中。当我刷新页面时,最后从设备获取的日期显示在文本字段中。 每次按下提交按钮时是否可以显示日期?

【讨论】:

    猜你喜欢
    • 2013-05-18
    • 1970-01-01
    • 2010-12-19
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多