如何在RoR中访问Jquery的Ajax调用? [英] How to access Jquery's Ajax call in RoR?

查看:58
本文介绍了如何在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) {

    }
  });

这篇关于如何在RoR中访问Jquery的Ajax调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆