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 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.
try this :
create or define a method which you want to use for your ajax request.
So in your controller, you can have
@my_item = Item.find(params[:id])
respond_to do |format|
make sure your method is available through routes
in your routes.rb you could have something like:
resources :model_name do
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
alert('I am doing fine');
and the beautifull part of this (besides the unobtrosiveness) is that you can also use Ruby code, like rendering partials.
Ruby on Rails made ajax childs play...
Last edited by rachid (2010-09-28 08:08:32)