Topic: Simple ajax

I have such view:

<h1>Ajax show</h1>
Click this link to show the current
<%= link_to 'time', {:action => 'time', :update => 'time_div'}, :remote => true %>
<div id='time_div'>
</div>

controller:

class PagesController < ApplicationController
  def formula
  end

  def time
     render :text => "The current time is #{Time.now.to_s}"
  end
end

And I add <%= javascript_include_tag :defaults %> to layout

When I click on the link "time" I get AJAX-request, but div id="time_id" doesn`t change. Why?

Re: Simple ajax

I used another function:

<%= link_to_remote('Some label', :update=> 'div_to_update',:url => project_users_path(@project), :method => :get)%>

Is that a typo:

but div id="time_id" doesn`t change. Why?

In your case you have 'time_div' instead.

Last edited by Javix (2010-10-20 05:32:54)

Re: Simple ajax

I am sorry, I use rails 3

Re: Simple ajax

I was playing around with this too and this is how I was able to get it to work (not sure if that is the correct approach). I was having trouble passing the action to it so I just setup a named route for the time being.

Routes

match 'time', :to => 'people#time'

View

Click this <%= link_to "link", time_path, :remote => true %> to show the current time.
<%= content_tag :div, '', :id => 'time_div' %>

Controller

  def  time
    render :update do |page|
      page.replace_html 'time_div', Time.now
      page.visual_effect :highlight, 'time_div', :duration => 3
    end            
  end

I tried to have the controller action call time.js.erb but that was unsuccessful, I assume I didn't have it setup properly.