2011-04-14 92 views
1

我正在使用link_to:remote来更新HTML上的div元素之一。这只是一个初学者的代码。但是,点击链接时不会发生更新。下面是代码:使用link_to问题:远程 - Rails 3

class SampleController < ApplicationController 
    def updater 
     render :text => Time.now 
    end 
end 

这是list.html.erb:

<script type = "text/javascript" src = "/javascripts/prototype.js"> 
<%= link_to "Hello Testing", 
    :update => "test_id", 
    :url => {:action => 'updater'}, 
    :remote => true 
%> 
<div id="test_id"></div> 

于是就点击链接 “您好测试”,在浏览器的网址更改为: http://localhost:3000/samples?remote=true&update=response_id&url[action]=updater

但是,我试图设置为当前时间的div元素不会在UI上发生。这里可能是什么问题?


更新了帖子:

的routes.rb:http://pastebin.com/wmKsa1DD 生成的HTML代码:在Firebug http://pastebin.com/stU3FpL8 HTML响应:http://pastebin.com/WjsB7zAh

使用url_for不会改变的行为。

+0

看起来好像没有创建正确的链接。你检查了链接生成的html代码吗? – 2011-04-14 06:43:30

+0

显示您的路线样本资源 – fl00r 2011-04-14 07:45:30

+0

这是因为:URL选项没有意义link_to:http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to – 2011-04-14 08:01:00

回答

0

1:你应该还包括文件rails.js。普通Rails中它使这样的:

<%= javascript_include_tag :defaults %> 

2:我更喜欢使用这种命名对于网址:updater_samples_path[:updater, :samples]

3:你应该表现出你的路线。如果使用不GET方法updater方法,那么你应该把它定义:

<%= link_to "Hello Testing", :update => "test_id", updater_samples_path, :method => :put, :remote => true %> 

4:使用萤火虫瑟您的AJAX响应。所以你可以轻松调试你的应用程序。你也可以向我们展示它的反应,所以我们会更好地把握局面

1

请使用url_for:

<%= link_to "Hello Testing", url_for(:action => :updater), 
     :update => "test_id", :remote => true %> 

我不是100%肯定,但我不认为:更新是去工作,因为现在Rails3中主要依靠UJS。

的 “轨道的方式” 来更新您的div会是什么样子(jQuery的):

$('div#test_id').bind('ajax:complete', function(event, xhr) { 
    $(this).html($.parseJSON(xhr.responseText)); 
})