2016-05-19 6 views
0

私のRailsアプリケーションでは、以下のように同様の関係を設定しようとしています。 Rails Object Relationships and JSON RenderingネストされたJSONとオブジェクトの関係をレンダリングする

私は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] 
     } 
    } 
] 

以下に示すように、私はすでに、モデルAのために必要なJSONを作成するコントローラを持っていますが、私はその情報を別のJSONセットに入れ子にするためにModelBモデルとコントローラを作成する方法や、どの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 

任意のリソースまたはヘルプがあります。 @alexbezekとして

+1

私はあなたが誤ってこのついて行くと思います。あなたのモデルはなぜJsonという名前ですか? ModelB.find(params [:id]) @ models.to_json(:include =>:modela) モデルコードを投稿すると、モデルのコードを投稿することができます。あなたがそれについてチャットしたいのであれば、気軽にメッセージにしてください –

+0

私はそれをmodelBに変更しました。あなたが指定しているコードはどこに置くのですか?私のモデルコードは、参照されるリンクに示されているように、単純にhas_many、belongs_to関係です。ありがとう – Kevin

答えて

0

は、すでにあなたがコントローラでModelBを取得する必要はなく、Json

RailsはJSONオブジェクトをレンダリングすることができます偉大な宝石jbuilderを持っていると述べました。各jsonアクションのビューを作成するだけです。ここで

は一例です:

# Controller 
# ... 
def index 
    @models_b = ModelB.all 
end 
... 

とビュー

#views/model_b/index.jbuilder 

json.array! @models_b do |item| 
    json.id item.id, 
    json.modela: item.models_a 
end 

それとも、可能なアプローチとして、あなたはJSONとして表示したいのですが、各モデル内のメソッドas_jsonを定義することができます。これは、モデルをjsonオブジェクトとしてレンダリングするための「手動の非遅延型」の方法です。例えば

# 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 
... 
+0

あなたの助けをありがとうが、私はあなたの例に従っており、それらをテストして、まだ理解していない。あなたの例のmodelbはmodelAからnestにjsonをどうやって取得しますか? – Kevin

+0

そこで、私はas_jsonソリューションを動作させ始めましたが、ModelBではModelEが未定義のローカル変数/メソッドであると言うNameErrorを取得しています。これを修正する方法はありますか? – Kevin

+0

更新:def as_jsonメソッドを使用してJSONをネストすることはできましたが、model_aオブジェクトがmodel_bオブジェクトに属する場所を関連付けるのではなく、すべてのmodel_bオブジェクトにネストされたすべてのmodel_aオブジェクトを返します。has_many – Kevin

関連する問題