2
내 컨트롤러의 색인 동작에서 JSON으로 렌더링해야하는 그림 모델 배열을 반환합니다.렌더링 레일 다형성 연관 객체를 JSON에
def index
@pictures = Pictures.all
respond_to do |format|
format.json { render json: @pictures.to_json(include: [:imageable]) }
end
end
이 모델은 다형성 연관으로 구성됩니다.
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
class Employee < ActiveRecord::Base
has_many :pictures, :as => :imageable
attr_accessible :name
end
class Product < ActiveRecord::Base
has_many :pictures, :as => :imageable
attr_accessible :type
end
사진 배열에는 Employee와 Product 모두에 이미지 가능하게 연결된 그림 개체가 포함됩니다. json에 이미지 가능 연결 객체 을 다르게 표현하려면에는 Employeee 및 Product 고유 필드가 모두 포함됩니까? 나는 당신이 당신의 JSON 응답을 구축 JBuilder 같은 것을 사용하는 것이 좋습니다
큰 접근! 나는 jbuilder로 뛰어 들었다. –