如何重定向到 Rails 3 中不同控制器中的特定操作?
我正在写一个API;在此 API 中,我有一个包含 Fruits
的 FruitBasket
模型。一种特定的水果一次可能属于多个水果篮。 FruitBasket
和 Fruit
都是 ActiveRecord
对象。
如果有人对 /fruit/100/baskets
执行 GET
,我想以篮子 ID 的形式提供包含该水果的篮子的 JSON 列表。如果只有一个购物篮,我想重定向到 /basket/x
,其中 x
是购物篮的 id。像这样的事情:
class FruitsController < ApplicationController
respond_to :json
def baskets
@baskets = Fruit.find(params[:id]).baskets
if baskets.size == 1
# What goes here?
else
respond_with @baskets
end
end
end
我应该在我的 routes
和 FruitsController
中放入什么来实现这个目标?
I'm writing an API; in this API, I have a FruitBasket
model which has Fruits
. A particular Fruit may belong to more than one FruitBasket at a time. FruitBasket
and Fruit
are both ActiveRecord
objects.
If someone performs a GET
on /fruit/100/baskets
, I want to provide a JSON list of baskets which have that fruit, in the form of basket IDs. If there's only one basket, I want to redirect to /basket/x
, where x
is the id of the basket. Something like this:
class FruitsController < ApplicationController
respond_to :json
def baskets
@baskets = Fruit.find(params[:id]).baskets
if baskets.size == 1
# What goes here?
else
respond_with @baskets
end
end
end
What do I put in my routes
and the FruitsController
to pull this off?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您是否真的想将它们重定向到那里,但如果您必须:
但是,希望 API 能够简单地返回一组篮子,如果只有一个篮子,那么它将是一个包含一项的数组。
I'm not sure you really want to redirect them over there, but if you must:
However, would expect an API to simple return an array of Baskets, and if there was only one basket, then it would be an array with one item.