Topic: How to access Jquey's Ajax call in RoR?

Guys I'm really novice to this RoR, and at this moment i reached to complex situation that "how to use Jquery's ajax() call in RoR?"

I've a controller called Projects like this

class ProjectsController < ApplicationController
    def stagemilestone
      @milestones=Milestone.find_by_sql("SELECT name, created_at FROM milestones WHERE stage=1")
    end
end

and i want to call this action from jquery's ajax call and return the data, for this I'm using like this

$.ajax({
  url: "/projects/stagemilestone",
  success: function(){
   //here i need the returned data from controller
   // means output of @milestones
  }
});

So please help me, how to do this?

Last edited by Shreekumar (2011-09-20 04:41:20)

Re: How to access Jquey's Ajax call in RoR?

You can't really do exactly what you want.  JQuery can't directly access controller data in the way I think you imagine it.

Normally , the success function would be used to say disable a wait...   spinner, or display a message that the Ajax call is over and it's time to deal with the results.

Here is one approach (of many)

Put the view code that displays the @milestones data in a partial. 

Then, in your controller,  after you populate @milestones, you'd render  that partial

class ProjectsController < ApplicationController
    def stagemilestone
      @milestones=Milestone.find_by_sql("SELECT name, created_at FROM milestones WHERE stage=1")
      render :partial => 'milestone'
    end
end

If you wish,  you could develop the partial to display only ONE milestone, and have that partial applied repeatedly to a collection of milestones, like:

class ProjectsController < ApplicationController
    def stagemilestone
      @milestones=Milestone.find_by_sql("SELECT name, created_at FROM milestones WHERE stage=1")
      render :partial => 'milestone', :collection=> @milestones
    end
end
Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: How to access Jquey's Ajax call in RoR?

Thanks for quick response BradHodges

I've updated my code like this

 def stagemilestone
  @milestones=Milestone.find_by_sql("SELECT name FROM milestones WHERE stage_id=1")
  respond_to do |format|
    format.html # index.html.erb
    format.json  { render :json => @milestones}
  end
end
$.ajax({
  type : "POST",
  contentType : "application/json; charset=utf-8",
  url: "/projects/stagemilestone",
  dataType : "json",
  success : function(json) {
   //here i need the returned data from controller
   // means output of @milestones
   var obj = jQuery.parseJSON(json);
   alert(obj);          
  }
});

It's working, But I'm not able to parse the JSON data properly... !!

Re: How to access Jquey's Ajax call in RoR?

Finally This is working for me

def stagemilestone
    @milestones=Milestone.find(:all, :conditions => ["status_id=? and project_id=?",params[:stageid], params[:id]])
    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @milestones}
    end
end
$.ajax({
    type : 'get',
    url : "/projects/stagemilestone",
    data : "stageid=" + $(this).attr('name') + "&id=" + $.cookie('projectid'),    
    dataType : 'json',
    async : false,
    context : document.body,
    success : function(response) {
        // my Code working great here :)
    }
  });

Last edited by Shreekumar (2011-10-23 11:17:33)