简体   繁体   English

使用link_to:remote的问题-Rails 3

[英]Issue with using link_to :remote - Rails 3

I am using link_to :remote to update one of the div elements on the HTML. 我正在使用link_to:remote更新HTML上的div元素之一。 This is just a beginner's code. 这只是一个初学者的代码。 However, the update does not occur on clicking the link. 但是,单击链接不会发生更新。 Here is the code: 这是代码:

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

this is the list.html.erb: 这是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>

So on click the link "Hello Testing", the URL in the browser changes to: http://localhost:3000/samples?remote=true&update=response_id&url[action]=updater 因此,单击链接“ Hello Testing”,浏览器中的URL更改为: http://localhost:3000/samples?remote=true&update=response_id&url[action]=updater

However, the div element that I am trying to set to the current time does not occur on the UI. 但是,我尝试设置为当前时间的div元素不会出现在UI上。 What can be the issue here? 这可能是什么问题?


Updated the post with: 使用以下内容更新了帖子:

routes.rb: http://pastebin.com/wmKsa1DD Generated HTML Code : http://pastebin.com/stU3FpL8 HTML Response in Firebug: http://pastebin.com/WjsB7zAh route.rb: http ://pastebin.com/wmKsa1DD生成的HTML代码: http ://pastebin.com/stU3FpL8 Firebug中的HTML响应: http ://pastebin.com/WjsB7zAh

Using url_for does not change the behaviour. 使用url_for不会更改行为。

please use url_for: 请使用url_for:

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

I'm not 100% sure, but I don't think that :update is going to work, since Rails3 now mainly relies on ujs. 我不确定100%,但我不认为:update会起作用,因为Rails3现在主要依赖ujs。

The "rails way" to update your div would look like (jquery) : 更新您的div的“ rails方法”看起来像(jquery):

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

1: You should include also rails.js file. 1:您还应该包括rails.js文件。 Ordinary in Rails it makes like this: 在Rails中,它是这样的:

<%= javascript_include_tag :defaults %>

2: I prefer to use this naming for urls: updater_samples_path or [:updater, :samples] 2:我更喜欢对网址使用此命名: updater_samples_path[:updater, :samples]

3: You should show your routes. 3:您应该显示路线。 If updater method using not GET method, then you should define it: 如果updater方法未使用GET方法,则应定义它:

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

4: Use FIREBUG to se your AJAX responses. 4:使用FIREBUG来检查您的AJAX响应。 So you can easily debug your app. 因此,您可以轻松调试您的应用程序。 Also you can show us its response, so we will better understend situation 您也可以告诉我们它的回应,这样我们会更好地了解情况

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM