Alexandre
Alexandre

Reputation: 13308

Rails - trying to create simple ajax example

The code below is not working for some reason. As I click at "test" it redirects me to /home/test and shows nothing.

view/home/index.html.erb

<%= link_to "test", { :controller => "home", :action => "test" }, :remote => true %>
   <div id='test_div'>
</div>

view/home/index.js.erb

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

controller

class HomeController < ApplicationController
 def test
    "test123 test456"
    respond_to() do |format|
      format.js {render :layout => false}
    end
  end
end

What should I do to refresh div_test using ajax?

Upvotes: 0

Views: 795

Answers (1)

sbolel
sbolel

Reputation: 3526

What does your routes file look like? Can you post it on here? Do a rake routes and post the output.

Here are some things that may help you:

  1. Rails documentation for routing
  2. Also, check out the Ajax on Rails Documentation and the section on link_to_remote. The link_to_remote function is not in Rails. What version of rails are you on? Bash: $ rails -v
  3. Look at this answer on S.O. regarding AJAX on Rails calls.
  4. Edit: Here is another answer regarding this that may help.
  5. Another answer on SO for updating existing element

Edit: looking over this again, I dont think you need () after respond_to or the {...} in link_to. Look at the third link. I think you need to put the page in a partial and render it. Its not rendering right now since its render :layout => false

Upvotes: 1

Related Questions