2

I have a controller:

class QuestionsController < ApplicationController
  def hello
    puts "====================!!!==================="
    @hello = "hey"
    "some"
  end

  def test
  end

test.rhtml:

<%= javascript_include_tag :defaults %>

<br/>
Click this link to show the current 
<br/>

<%= link_to "hello", 
    { :controller => "questions", :action => "hello" },
    :update => 'time_div', 
    :remote => true %>.

<br/>
<div id='time_div'>
  ...
</div>

When I click on 'hello' link I see that hello() method was called, but html page remains the same. Why?

How do I need to change code to update HTML page?

Here is generated HTML page:

<!DOCTYPE html>
<html>
<head>
  <title>Stack</title>


  <script src="/javascripts/jquery.js?1286229922" type="text/javascript"></script>
<script src="/javascripts/jquery-ujs.js?1286229922" type="text/javascript"></script>
  <meta name="csrf-param" content="authenticity_token"/>
<meta name="csrf-token" content="UKPX1dNCZhyTk8u71hR9KaUmufIire7Rhvg8t7cRSlM="/>

</head>

<body>



<br/>
Click this link to show the current 
<br/>

<a href="/questions/hello" data-remote="true">hello</a>
<br/>

<div id='time_div'>
  ...
</div>



</body>
</html>
ceth
  • 44,198
  • 62
  • 180
  • 289

2 Answers2

4

You can have your hello action like this:

def hello
  #### your code goes here #####
  respond_to do |format|
    format.js { render :layout=>false }
  end
end

And one file hello.js.erb

$("#time_div").html("some text");

And your link will be:

<%= link_to "hello", { :controller => "questions", :action => "hello" }, :remote => true %>.
Ashish
  • 5,723
  • 2
  • 24
  • 25
  • 1. Where do I need to put hello.js.erb? view\questions\hello.js.erb ? 2. Do I need to install jQuery? – ceth Apr 05 '11 at 10:30
  • Yes... you need jquery. And you need to keep hello.js.erb in /views/questions folder – Ashish Apr 05 '11 at 10:34
  • I cannot make it work. I will be thankfull if you look at my code (https://github.com/demas/test_ruby_ajax) and give me information about my mistake. – ceth Apr 05 '11 at 10:54
  • I think you need rails.js for jquery. You still have rails.js for prototype. Just update it and include it in your layout file. – Ashish Apr 05 '11 at 12:02
  • It doesn't help https://github.com/demas/test_ruby_ajax/commit/95f2a19e2b1918a1db084e84ce57ff7345b1cce8 – ceth Apr 05 '11 at 12:34
  • You are still using prototype version of rails.js...Update this file for JQuery version. And you dont need to include application.js file – Ashish Apr 05 '11 at 12:38
  • Like this https://github.com/demas/test_ruby_ajax/commit/41f4e24ecbacb802fc1bb18e2746ae34e0cdd270#diff-0 – ceth Apr 05 '11 at 12:47
  • Use $("#time_div").html("some text"); in your hello.js.erb file and check – Ashish Apr 05 '11 at 12:56
  • Added rails.js but still same issue :) added to manifest and restarted – Rubytastic Oct 14 '13 at 07:37
3

problem here is you are not returning anything from the controller action, Since you are using AJAX you need to return the result as either .js or jason

have a look at this example, with rails3 and Jquery

https://github.com/nu7hatch/rails3-ujs-example

cheers

sameera

sameera207
  • 16,547
  • 19
  • 87
  • 152