Guest User

Untitled

a guest
May 27th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. ## router.rb
  2. Merb::Router.prepare do
  3. resources :comments
  4. resources :posts do
  5. member :add_comment, :method => :post
  6. end
  7.  
  8. ...
  9. end
  10.  
  11. ## show.html.haml
  12. = form_for @comment, :action => resource(@post, :add_comment), :method => :post do
  13. .input
  14. = text_field :name => "posted_by", :label => "Posted By"
  15. .input
  16. = text_field :name => "url", :label => "Website"
  17. .input
  18. = text_field :name => "email", :label => "Email"
  19. .input
  20. = text_area :name => "body", :label => "Comment"
  21. .input
  22. = submit "Post Comment"
  23.  
  24. ## posts.rb (controller)
  25. class Posts < Application
  26. # provides :xml, :yaml, :js
  27. before :ensure_authenticated #, :only => [:index :edit :create :update :destroy]
  28.  
  29. def index
  30. @posts = Post.all
  31. display @posts
  32. end
  33.  
  34. def show(id)
  35. @post = Post.get(id)
  36. raise NotFound unless @post
  37. @comment = Comment.new
  38. display @post
  39. end
  40.  
  41. def new
  42. only_provides :html
  43. @post = Post.new
  44. display @post
  45. end
  46.  
  47. def edit(id)
  48. only_provides :html
  49. @post = Post.get(id)
  50. raise NotFound unless @post
  51. display @post
  52. end
  53.  
  54. def create(post)
  55. @post = Post.new(post)
  56. if @post.save
  57. redirect resource(@post), :message => {:notice => "Post was successfully created"}
  58. else
  59. message[:error] = "Post failed to be created"
  60. render :new
  61. end
  62. end
  63.  
  64. def update(id, post)
  65. @post = Post.get(id)
  66. raise NotFound unless @post
  67. if @post.update_attributes(post)
  68. redirect resource(@post)
  69. else
  70. display @post, :edit
  71. end
  72. end
  73.  
  74. def destroy(id)
  75. @post = Post.get(id)
  76. raise NotFound unless @post
  77. if @post.destroy
  78. redirect resource(:posts)
  79. else
  80. raise InternalServerError
  81. end
  82. end
  83.  
  84. def add_comment(id)
  85. @post = Post.get(id)
  86. raise NotFound unless @post
  87.  
  88. =begin ## Something I was playing with
  89. #props = Comment.properties.map {|p| p.name}
  90. #c_params = params.select {|k,v| props.include?(k)}
  91. #raise c_params.to_yaml
  92. #@comment = @post.comments.build(c_params)
  93. =end
  94.  
  95. #@comment = @post.comments.build(params) #=> Does not work
  96. @comment = @post.comments.build( # ugly but it works
  97. :posted_by => params[:posted_by],
  98. :email => params[:email],
  99. :body => params[:body]
  100. ## Other params here
  101. )
  102. if @comment.save
  103. message[:message] = "Comment Created"
  104. else
  105. message[:error] = "Comment failed to be created"
  106. end
  107. redirect resource(@post)
  108. end
  109. end # Posts
Add Comment
Please, Sign In to add comment