【问题标题】:New To Rails 3, ajax request with json responseRails 3 的新手,带有 json 响应的 ajax 请求
【发布时间】:2011-03-07 18:39:54
【问题描述】:

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

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

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

  3. respond_to 做什么? (我见过它块样式,我理解,但是非块样式呢)

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 json


    【解决方案1】:
    1. 是的,你的 JS 会按照以下方式做一些事情:

      $.getJSON("/course_requests/new.json",...);
      
    2. 您不必这样做,您只需要在其中处理 JSON 的 respond_to 块。

      respond_to do |format|
        format.json { render :json => "test" }
      end
      
    3. 无块语法,我想你的意思是:http://davidwparker.com/2010/03/09/api-in-rails-respond-to-and-respond-with/。基本上,您指定控制器响应的 mime 类型,然后您可以使用更酷的 responds_with 方法。

    【讨论】:

    猜你喜欢
    • 2012-02-24
    • 2011-04-30
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2013-09-29
    • 2018-01-21
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多