新到Rails 3,带有json响应的ajax请求 [英] New To Rails 3, ajax request with json response

查看:182
本文介绍了新到Rails 3,带有json响应的ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为CourseRequests的控制器,它将接受新"方法的ajax请求.

I have a controller named CourseRequests which will be accepting an ajax request for the "new" method.

  1. 由于它将以json响应,我应该使用/course_requests/new.json吗?

  1. Since it will be responding with json, should I use /course_requests/new.json?

我不想为如此愚蠢的json响应创建模板,我该怎么做?

I don't want to make a template for such a silly json response, how would I do that?

response_做什么? (我已经看到了它的块样式,并且我理解这一点,但是非块样式呢?)

What does respond_to do? (I have seen it block style and I understand that, but what about non-block style)

推荐答案

  1. 是的,您的JS将按照以下方式进行操作:

  1. Yes, your JS would be doing something along the lines of:

$.getJSON("/course_requests/new.json",...);

  • 您不必这样做,只需要在其中处理JSON的response_to块即可.

  • You don't have to, you just need to have a respond_to block with JSON handled there.

    respond_to do |format|
      format.json { render :json => "test" }
    end
    

  • 无障碍语法,我想您是说这句话: http://davidwparker.com/2010/03/09/api-in-rails-respond-to-and-respond-with/.基本上,您可以指定控制器响应的mime类型,然后可以使用更酷的responses_with方法.

  • Blockless syntax, I think you mean this: http://davidwparker.com/2010/03/09/api-in-rails-respond-to-and-respond-with/ . Basically, you specify what mime types your controller responds to and then you can use the cooler responds_with method.

    这篇关于新到Rails 3,带有json响应的ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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