如何解决使用link_to:remote-Rails 3的问题
|| 我正在使用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));
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。