【发布时间】:2014-09-03 17:59:17
【问题描述】:
我正在用 ruby on rails 做一个 API。我有 2 种返回 json 资源的方法。这两个资源彼此没有任何关系。我想要做的是构建一种方法来在一个请求中返回这两个资源。在不损害宁静原则的情况下,最好的方法是什么?我应该保持这种方式吗?
编辑
我的文件可以更好地解释我自己。
routes.rb
resources :cities, only: [:index]
resources :products, only: [:index]
cities_controller.rb
class CitiesController < ApplicationController
respond_to :json
def index
@cities = City.all
render :json => @cities
end
end
products_controller.rb
class ProductsController < ApplicationController
respond_to :json
def index
@products = Product.all
render :json => @products
end
end
我应该创建第三个控制器和路由以将这两个资源放在一个 JSON 中吗?这是更好的方法吗?
【问题讨论】:
标签: ruby-on-rails rest restful-architecture