JSON을 통해 모든 기사의 색인을 렌더링하고 싶습니다.Ruby on Rails 고급 JSON 직렬화
@index = Article.find(:all, :select => 'id, title')
나 '유일한 방법 : 내가 응답 할 싶은 무엇
if params[:id]
@article = Article.find(params[:id])
else
@article = Article.published.not_draft.by_recent.first
end
respond_to do |format|
format.js { render :json => @article.to_json(
:except => [ :created_at, :updated_at, :draft, :id, :publish ],
:include => {
:comments => {
:only => [:body]
}
}),
:callback => params[:callback]}
end
지금처럼 모든 기사의 인덱스를 추가 할 수 있습니다 : 여기
지금 내 컨트롤러 인덱스와 기사를 해시 또는 배열에 넣은 다음이를 JSON에 저장합니다. 모두@response = { :item => @article, :index => @index }
전체 코드 : 지금은 :include
또는 :except
을 지정하고 제대로 렌더링 얻을 수 없습니다 제외
@index = Article.find(:all, :select => 'id, title')
if params[:id]
@article = Article.find(params[:id])
else
@article = Article.published.not_draft.by_recent.first
end
@response = { :item => @article, :index => @index }
respond_to do |format|
format.js { render :json => @response.to_json(), :callback => params[:callback]}
end
이것은, 잘 될 것입니다.