Dam*_*son 14 api json idioms ruby-on-rails ruby-on-rails-3
我正在尝试使用对RESTful资源控制器的JSON响应为我的rails应用程序创建API.这对我来说是一次全新的体验,所以我正在寻找一些指导和指导.开始时:
附加信息:
yfe*_*lum 29
#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 !@article.valid? then respond_with
# will show the :edit view, including @article.errors
respond_with @article
end
...
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14190 次 |
| 最近记录: |