问题描述
||
我正在使用link_to:remote更新HTML上的
div
元素之一。这只是一个初学者的代码。但是,单击链接不会发生更新。这是代码:
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>
因此,单击链接“ Hello Testing”,浏览器中的URL更改为:
http://localhost:3000/samples?remote=true&update=response_id&url[action]=updater
但是,我尝试设置为当前时间的“ 0”元素在UI上没有发生。这可能是什么问题?
使用以下内容更新了帖子:
routes.rb:http://pastebin.com/wmKsa1DD
生成的HTML代码:http://pastebin.com/stU3FpL8
Firebug中的HTML响应:http://pastebin.com/WjsB7zAh
使用url_for
不会改变行为。
解决方法
1:还应包括“ 6”文件。在Rails中,它是这样的:
<%= javascript_include_tag :defaults %>
2:我更喜欢对网址使用此命名:updater_samples_path
或[:updater,:samples]
3:您应该显示路线。如果updater
方法未使用GET
方法,则应定义它:
<%= link_to \"Hello Testing\",:update => \"test_id\",updater_samples_path,:method => :put,:remote => true %>
4:使用FIREBUG来检查您的AJAX响应。因此,您可以轻松调试您的应用程序。您也可以向我们展示其回应,以便我们更好地了解情况
,请使用url_for:
<%= link_to \"Hello Testing\",url_for(:action => :updater),:remote => true %>
我不确定100%,但是我不认为:update可以正常工作,因为Rails3现在主要依赖ujs。
用于更新div的\“ rails way \\”看起来像(jquery):
$(\'div#test_id\').bind(\'ajax:complete\',function(event,xhr) {
$(this).html($.parseJSON(xhr.responseText));
})