New To Rails 3,带有json响应的ajax请求

时间:2022-08-06 20:15:18

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

我有一个名为CourseRequests的控制器,它将接受“new”方法的ajax请求。

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

    既然它将用json响应,我应该使用/course_requests/new.json吗?

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

    我不想为这种愚蠢的json响应制作模板,我该怎么做?

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

    respond_to做什么? (我已经看到了它的块样式,我明白了,但是非块样式呢)

1 个解决方案

#1


10  

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

    是的,你的JS会做的事情是这样的:

    $.getJSON("/course_requests/new.json",...);
    
  2. You don't have to, you just need to have a respond_to block with JSON handled there.

    您不必,您只需要在其中处理带有JSON的respond_to块。

    respond_to do |format|
      format.json { render :json => "test" }
    end
    
  3. 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.

    无块语法,我认为你的意思是:http://davidwparker.com/2010/03/09/api-in-rails-respond-to-and-respond-with/。基本上,您指定控制器响应的mime类型,然后您可以使用冷却器responds_with方法。

#1


10  

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

    是的,你的JS会做的事情是这样的:

    $.getJSON("/course_requests/new.json",...);
    
  2. You don't have to, you just need to have a respond_to block with JSON handled there.

    您不必,您只需要在其中处理带有JSON的respond_to块。

    respond_to do |format|
      format.json { render :json => "test" }
    end
    
  3. 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.

    无块语法,我认为你的意思是:http://davidwparker.com/2010/03/09/api-in-rails-respond-to-and-respond-with/。基本上,您指定控制器响应的mime类型,然后您可以使用冷却器responds_with方法。