如何在RoR中访问Jquery的Ajax调用?

问题描述:

伙计们,我真的是RoR的新手,此刻我遇到了一个复杂的情况,即如何在RoR中使用Jquery的ajax()调用?"

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?"

我有一个名为Projects的控制器

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

我想从jquery的ajax调用中调用此操作并返回数据,为此,我正在这样使用

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?

最后我找到了Solutions as Follows并且工作很棒!

Guys finally i found Solution as Follows and working Great!!

控制器

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

和我的 char.js 看起来像这样

$.ajax({
    type : 'get',
    url : "/projects/stagemilestone",
    data : "stageid=" + $(this).attr('name') + "&id=" + $.cookie('projectid'),    
    dataType : 'json',
    async : false,
    context : document.body,
    success : function(response) {

    }
  });