Guest User

Untitled

a guest
Aug 3rd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. Rails 3 - JSON of nested models not returning as expected
  2. > [{"content":"My first
  3. > post","created_at":"2012-02-07T18:56:16Z","id":1,"title":"Hello","updated_at":"2012-02-07T18:56:16Z"},{"content":"More
  4. > crap","created_at":"2012-02-07T21:30:51Z","id":2,"title":"My 2nd
  5. > Post","updated_at":"2012-02-07T21:30:51Z"}]
  6.  
  7. > {"content":"that's
  8. > nice","created_at":"2012-02-07T20:57:16Z","id":4,"post_id":1,"title":"cool","updated_at":"2012-02-07T20:57:16Z"}
  9.  
  10. class CommentsController < ApplicationController
  11.  
  12. before_filter :get_post
  13.  
  14. respond_to :html, :xml, :json
  15.  
  16. def index
  17. @comments = @post.comments.all
  18. respond_with (@comment)
  19. end
  20.  
  21. def show
  22. @comment = @post.comments.find(params[:id])
  23.  
  24. respond_with(@comment)
  25.  
  26. end
  27.  
  28. def new
  29. @comment = @post.comments.build
  30.  
  31. respond_with(@comment)
  32. end
  33.  
  34. def edit
  35. @comment = @post.comments.find(params[:id])
  36. end
  37.  
  38. def create
  39. @comment = @post.comments.build(params[:comment])
  40.  
  41. respond_to do |format|
  42. if @comment.save
  43. format.html { redirect_to([@post, @comment], :notice => 'Comment was successfully created.') }
  44. format.xml { render :xml => @comment, :status => :created, :location => @comment }
  45. format.json { render :json => @comment, :status => :created, :location => @comment }
  46. else
  47. format.html { render :action => "new" }
  48. format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
  49. format.json { render :json => @comment.errors, :status => :unprocessable_entity }
  50. end
  51. end
  52. end
  53.  
  54. def update
  55. @comment = @post.comments.find(params[:id])
  56.  
  57. respond_to do |format|
  58. if @comment.update_attributes(params[:comment])
  59. format.html { redirect_to(@comment, :notice => 'Comment was successfully updated.') }
  60. format.xml { head :ok }
  61. format.json { head :ok }
  62. else
  63. format.html { render :action => "edit" }
  64. format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
  65. format.json { render :json => @comment.errors, :status => :unprocessable_entity }
  66. end
  67. end
  68. end
  69.  
  70. def destroy
  71. @comment = @post.comments.find(params[:id])
  72. @comment.destroy
  73.  
  74. respond_to do |format|
  75. format.html { redirect_to(post_comments_url) }
  76. format.xml { head :ok }
  77. format.json { head :ok }
  78. end
  79. end
  80.  
  81. protected
  82.  
  83. def get_post
  84. @post = Post.find_by_id(params[:post_id])
  85. redirect_to root_path unless @post
  86. end
  87.  
  88. end
  89.  
  90. def index
  91. @comments = @post.comments.all
  92. respond_with (@comment)
  93. end
Add Comment
Please, Sign In to add comment