Rails 3:在 Rails 中使用 JSON 响应 REST 式操作的正确方法是什么?

发布于 2024-08-27 06:04:20 字数 418 浏览 1 评论 0原文

我正在尝试使用对 RESTful 资源控制器的 JSON 响应为我的 Rails 应用程序创建一个 API。这对我来说是一种新的体验,所以我正在寻找一些指导和指示。首先:

  1. 在 Rails 应用程序中,使用 JSON 响应 REST-ful 控制器方法的“正确”方法是什么? (创建、更新、销毁)
  2. 是否有一种惯用的方式通过 JSON 响应来指示成功/失败?

其他信息:

  • 我目前正在使用rails 3.0.beta2,
  • 我想避免使用插件或gem来完成繁重的工作,我的目标是更好地了解如何制作rails 3 API。
  • 指向我可以找到有关该主题的更多信息的地方的链接也将受到赞赏,在谷歌上进行一些快速搜索并没有给我带来多大好处。

I'm trying to make an API for my rails application using JSON responses to RESTful resource controllers. This is a new experience for me, so I'm looking for some guidance and pointers. To start things off:

  1. In a rails application, what is the "proper" way to respond with JSON to REST-ful controller methods? (create, update, destroy)
  2. Is there an idiomatic way to indicate success/failure through a JSON response?

Additional information:

  • I'm currently working with rails 3.0.beta2
  • I would like to avoid using a plugin or gem to do the grunt work, my goal is to gain a better understanding of how to make a rails 3 API.
  • Links to places I could find more information on the topic would also be appreciated, some quick searching on google didn't do me much good.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假面具 2024-09-03 06:04:20
#config/routes.rb
MyApplicationsName::Application.routes.draw do
  resources :articles
end

#app/controllers/articles_controller.rb
class ArticlesController < ActionController::Base

  # so that respond_with knows which formats are
  # allowed in each of the individual actions
  respond_to :json

  def index
    @articles = Article.all
    respond_with @articles
  end

  def show
    @article = Article.find(params[:id])
    respond_with @article
  end

  ...

  def update
    @article = Article.find(params[:id])
    @article.update_attributes(params[:article])

    # respond_with will automatically check @article.valid?
    # and respond appropriately ... @article.valid? will
    # be set based on whether @article.update_attributes
    # succeeded past all the validations
    # if @article.valid? then respond_with will redirect to
    # to the show page; if [email protected]? then respond_with
    # will show the :edit view, including @article.errors
    respond_with @article
  end

  ...

end
#config/routes.rb
MyApplicationsName::Application.routes.draw do
  resources :articles
end

#app/controllers/articles_controller.rb
class ArticlesController < ActionController::Base

  # so that respond_with knows which formats are
  # allowed in each of the individual actions
  respond_to :json

  def index
    @articles = Article.all
    respond_with @articles
  end

  def show
    @article = Article.find(params[:id])
    respond_with @article
  end

  ...

  def update
    @article = Article.find(params[:id])
    @article.update_attributes(params[:article])

    # respond_with will automatically check @article.valid?
    # and respond appropriately ... @article.valid? will
    # be set based on whether @article.update_attributes
    # succeeded past all the validations
    # if @article.valid? then respond_with will redirect to
    # to the show page; if [email protected]? then respond_with
    # will show the :edit view, including @article.errors
    respond_with @article
  end

  ...

end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文