I am using link_to :remote to update one of the div
elements on the HTML. 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:
<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
However, the div
element that I am trying to set to the current time does not occur on the 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
Using url_for
does not change the behaviour.
please use 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.
The "rails way" to update your div would look like (jquery) :
$('div#test_id').bind('ajax:complete', function(event, xhr) {
$(this).html($.parseJSON(xhr.responseText));
})
1: You should include also rails.js
file. Ordinary in Rails it makes like this:
<%= javascript_include_tag :defaults %>
2: I prefer to use this naming for urls: updater_samples_path
or [:updater, :samples]
3: You should show your routes. If updater
method using not GET
method, then you should define it:
<%= link_to "Hello Testing", :update => "test_id", updater_samples_path, :method => :put, :remote => true %>
4: Use FIREBUG to se your AJAX responses. So you can easily debug your app. Also you can show us its response, so we will better understend situation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.