I'm trying to set up a javascript front in that is consuming JSON from a Rails backend.
The only problem is, when I have nested models in Rails, I'm not sure if the JSON being returned is in the correct format.
I have a Post model and a Comment model. A Post has many Comments. Each model has a 'title' and 'content' field.
Here's where my problem is so far...
1) When I visit the url: http://localhost:3000/posts.json I get the expected output:
> [{"content":"My first
> post","created_at":"2012-02-07T18:56:16Z","id":1,"title":"Hello","updated_at":"2012-02-07T18:56:16Z"},{"content":"More
> crap","created_at":"2012-02-07T21:30:51Z","id":2,"title":"My 2nd
> Post","updated_at":"2012-02-07T21:30:51Z"}]
2) If I put in this url: http://localhost:3000/posts/1/comments/4.json I also get the expected:
> {"content":"that's
> nice","created_at":"2012-02-07T20:57:16Z","id":4,"post_id":1,"title":"cool","updated_at":"2012-02-07T20:57:16Z"}
3) But... if I put in a url like this: http://localhost:3000/posts/1/comments.json
it returns null.
I don't have access to the JSON version of the list of Comments associated with the Post.
Using the standard Rails views, I can do full CRUD with the nesting no problem, but do I need to do something different from the way Rails normally passes the JSON back in order to consume it on the client side?
EDIT
I'm assuming you may need to see the controller code for the comments controller. Here it is:
class CommentsController < ApplicationController
before_filter :get_post
respond_to :html, :xml, :json
def index
@comments = @post.comments.all
respond_with (@comment)
end
def show
@comment = @post.comments.find(params[:id])
respond_with(@comment)
end
def new
@comment = @post.comments.build
respond_with(@comment)
end
def edit
@comment = @post.comments.find(params[:id])
end
def create
@comment = @post.comments.build(params[:comment])
respond_to do |format|
if @comment.save
format.html { redirect_to([@post, @comment], :notice => 'Comment was successfully created.') }
format.xml { render :xml => @comment, :status => :created, :location => @comment }
format.json { render :json => @comment, :status => :created, :location => @comment }
else
format.html { render :action => "new" }
format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
format.json { render :json => @comment.errors, :status => :unprocessable_entity }
end
end
end
def update
@comment = @post.comments.find(params[:id])
respond_to do |format|
if @comment.update_attributes(params[:comment])
format.html { redirect_to(@comment, :notice => 'Comment was successfully updated.') }
format.xml { head :ok }
format.json { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
format.json { render :json => @comment.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@comment = @post.comments.find(params[:id])
@comment.destroy
respond_to do |format|
format.html { redirect_to(post_comments_url) }
format.xml { head :ok }
format.json { head :ok }
end
end
protected
def get_post
@post = Post.find_by_id(params[:post_id])
redirect_to root_path unless @post
end
end
respond_with(@comments)or it's a typo?