I am trying to set up a relationship in my Rails application similar to the one in the following SO question. Rails Object Relationships and JSON Rendering
我试图在我的Rails应用程序中建立一个类似于以下SO问题中的关系。 Rails对象关系和JSON渲染
I want my JSON to be rendered in the same way as shown which is:
我希望我的JSON以与显示的相同的方式呈现:
[
{
"modelb": {
"id": "1",
"modela": [insert the ModelA JSON for ID's 1, 2 and 3]
}
{
"modelb": {
"id": "2",
"modela": [insert the ModelA JSON for ID's 3, 4 and 5]
}
}
]
I already have a controller that creates the JSON necessary for Model A, as shown below, but I do not know how to make the ModelB model and controller in order to nest that information into another set of JSON or how to specify which ModelA objects go into which of ModelB's objects.
我已经有一个控制器来创建模型A所需的JSON,如下所示,但我不知道如何创建ModelB模型和控制器以便将该信息嵌套到另一组JSON中或如何指定哪些ModelA对象去哪个是ModelB的对象。
class JsonsController < ApplicationController
before_action :set_json, only: [:show, :update, :destroy]
# GET /jsons
# GET /jsons.json
def index
@jsons = Json.all
render json: @jsons
end
# GET /jsons/1
# GET /jsons/1.json
def show
render json: @json
end
# POST /jsons
# POST /jsons.json
def create
@json = Json.new(json_params)
if @json.save
render json: @json, status: :created, location: @json
else
render json: @json.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /jsons/1
# PATCH/PUT /jsons/1.json
def update
@json = Json.find(params[:id])
if @json.update(json_params)
head :no_content
else
render json: @json.errors, status: :unprocessable_entity
end
end
# DELETE /jsons/1
# DELETE /jsons/1.json
def destroy
@json.destroy
head :no_content
end
private
def set_json
@json = Json.find(params[:id])
end
def json_params
params.require(:json).permit(:text, :parent, :id)
end
end
Any resources or help would be appreciated.
任何资源或帮助将不胜感激。
1 个解决方案
#1
0
As @alexbezek already said you need to get ModelB
in controller, but not Json
正如@alexbezek已经说过你需要在控制器中使用ModelB,而不是Json
Rails have a great gem jbuilder that allow to render a json objects. You just create views for each json action.
Rails有一个很棒的gem jbuilder,允许渲染json对象。您只需为每个json操作创建视图。
Here is the example:
这是一个例子:
# Controller
# ...
def index
@models_b = ModelB.all
end
...
And view
#views/model_b/index.jbuilder
json.array! @models_b do |item|
json.id item.id,
json.modela: item.models_a
end
Or as just a possible approach you could define method as_json
in each model you'd like to display as json. This is a "manual non-lazy" way to render your models as json objects.
或者作为一种可能的方法,您可以在每个要显示为json的模型中定义方法as_json。这是一种将模型渲染为json对象的“手动非惰性”方式。
For example:
# ModelB
def as_json
jsn = {
id: id,
name: name,
any_other_attr_you_need: any_other_attr_you_need
}
jsn[:modela] = models_a.as_json if models_a.any?
end
#ModelA
def as_json
jsn = {
id: id,
name: name,
etc: etc
}
jsn
end
# Controller
# ...
def index
@models_a = ModelA.all
render json: @models_a.as_json
end
...
#1
0
As @alexbezek already said you need to get ModelB
in controller, but not Json
正如@alexbezek已经说过你需要在控制器中使用ModelB,而不是Json
Rails have a great gem jbuilder that allow to render a json objects. You just create views for each json action.
Rails有一个很棒的gem jbuilder,允许渲染json对象。您只需为每个json操作创建视图。
Here is the example:
这是一个例子:
# Controller
# ...
def index
@models_b = ModelB.all
end
...
And view
#views/model_b/index.jbuilder
json.array! @models_b do |item|
json.id item.id,
json.modela: item.models_a
end
Or as just a possible approach you could define method as_json
in each model you'd like to display as json. This is a "manual non-lazy" way to render your models as json objects.
或者作为一种可能的方法,您可以在每个要显示为json的模型中定义方法as_json。这是一种将模型渲染为json对象的“手动非惰性”方式。
For example:
# ModelB
def as_json
jsn = {
id: id,
name: name,
any_other_attr_you_need: any_other_attr_you_need
}
jsn[:modela] = models_a.as_json if models_a.any?
end
#ModelA
def as_json
jsn = {
id: id,
name: name,
etc: etc
}
jsn
end
# Controller
# ...
def index
@models_a = ModelA.all
render json: @models_a.as_json
end
...