I'm looking to render an index of all articles along with a full article via json in my rails app, but I'm having a little trouble figuring out how to do it.
Here is my controller now:
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
What I'd like to do in the response is add an index of all articles, like so:
@index = Article.find(:all, :select => 'id, title')
The only way I've been able to do it, is put both the index and article into a hash or array and then put that to json.
@response = { :item => @article, :index => @index }
Full code with both:
@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
This would be fine, except now I cannot specify :include or :except and get it to render properly.
I'm really stuck here. Any help would be appreciated. Thanks.