Topic: jQuery ajax requests

Hi guys,

I want to execute a simple thing. When the user clicks the link jQuery needs to generate an Ajax request which sends the id of the link item to a method in the controller. In the method i respond with the js format and render a popup modal dialog box. The dialog works ok, but I can't generate the ajax request sad Don't know already how to deal with the jQuery.ajax method, especially how to force jQuery to pass the url parameter to the rails method. I watched the railscast and studied the example with .post, but I don't need to submit any form.
Any advise is appreciated.
Thanks

Re: jQuery ajax requests

dennis123 wrote:

Hi guys,

I want to execute a simple thing. When the user clicks the link jQuery needs to generate an Ajax request which sends the id of the link item to a method in the controller. In the method i respond with the js format and render a popup modal dialog box. The dialog works ok, but I can't generate the ajax request sad Don't know already how to deal with the jQuery.ajax method, especially how to force jQuery to pass the url parameter to the rails method. I watched the railscast and studied the example with .post, but I don't need to submit any form.
Any advise is appreciated.
Thanks

try this :

create or define a method which you want to use for your ajax request.

So in your controller, you can have

def my_ajax_method
    @my_item = Item.find(params[:id])
    respond_to do |format|
       format.js
       format.html
      end
  end
end

make sure your method is available through routes

in your routes.rb you could have something like:

  resources :model_name do
    collection do
        get :my_ajax_method
    end
  end

save it and confirm the path you need doing a
$ rake routes

now get the path used for this method and open the view you want to use.

<%= link_to("my link",my_ajax_method_somemodel_path(:js, :id => some_id),:remote => true) %>

pay attention to the first argument which is telling your controller what renderformat to use. in this case :js AND the :remote => true argument.

the last thing you need to do is to create a js.erb file
my_ajax_method.js.erb

containing

alert('I am doing fine');

in this file you can write all the javascript you need when clicked the link.
and the beautifull part of this (besides the unobtrosiveness) is that you can also use Ruby code, like rendering partials.

$("#some_selector").html("<%= escape_javascript(render(:partial => 'items/details')) %>")

Ruby on Rails made ajax childs play...

Last edited by rachid (2010-09-28 08:08:32)

Re: jQuery ajax requests

Thx rachid. Really helpfully post for me. I had a little issue during your tutorial. Exactly a copy-paste mistake.

In this line:

<%= link_to("my link",my_ajax_method_somemodel_path(:js, :id => some_id),remote => true) %>

The colon before the remote option^^

<%= link_to("my link",my_ajax_method_somemodel_path(:js, :id => some_id), :remote => true) %>

Again thanks!

Last edited by dpuglisi (2010-09-23 09:11:38)