Guest User

Untitled

a guest
Apr 22nd, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. ## posts / show.html.erb
  2.  
  3. <h1><%= h @post.title %></h1>
  4. <div class="post">
  5. <p class="created_at">Posted <%= pretty_datetime(@post.created_at) %></p>
  6. <p><%= sanitize @post.body %></p>
  7. </div>
  8.  
  9. <% unless @post.comments.empty? -%>
  10. <div class="comments">
  11. <h2>Comments</h2>
  12. <%= render :partial => @post.comments %>
  13. </div>
  14. <% end -%>
  15.  
  16. <div class="comment_form">
  17. <h2>Post a comment</h2>
  18. <%= error_messages_for :comment %>
  19. <% form_for :comment, :url => post_comments_path(@post.id) do |f| %>
  20. <div>Name</div>
  21. <div><%= f.text_field :author %></div>
  22. <div>Email <span class="small gray">&mdash; optional, shown to public</span></div>
  23. <div><%= f.text_field :email %></div>
  24. <div>Comment</div>
  25. <div><%= f.text_area :body, :size => "30x6" %></div>
  26. <div><%= submit_tag "Post comment" %></div>
  27. </div>
  28. <% end -%>
  29.  
  30. ## comments_controller.rb
  31.  
  32. class CommentsController < ApplicationController
  33.  
  34. def create
  35. post_id = params[:post_id]
  36. @post = Post.find(post_id)
  37. @comment = Comment.new(params[:comment])
  38. @comment.post_id = post_id
  39. if @comment.save
  40. # Validations passed
  41. flash[:notice] = "Your comment has been saved, #{@comment.author.titleize}."
  42. redirect_to post_path(@post.permalink)
  43. else
  44. render post_path(@post.permalink)
  45. end
  46. end
  47.  
  48. def new
  49. @comment = Comment.new
  50. end
  51.  
  52. end
  53.  
  54. ## comment.rb
  55.  
  56. class Comment < ActiveRecord::Base
  57. validates_presence_of :name, :comment
  58. validates_format_of :email, :with => /\A\w+\@\w+\.\w+\Z/, :unless => :email_blank?
  59. belongs_to :post, :counter_cache => true # comments.post_id
  60.  
  61. def email_blank?
  62. self.email.blank?
  63. end
  64. end
  65.  
  66. ## Error when submitting blank comment
  67.  
  68. ActionController::RenderError in CommentsController#create
  69.  
  70. You called render with invalid options : /posts/nullam-tincidunt
  71. RAILS_ROOT: /Users/kip/svn/doohickey
  72.  
  73. Application Trace | Framework Trace | Full Trace
  74. /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:841:in `render_with_no_layout'
  75. /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/layout.rb:262:in `render_without_benchmark'
  76. /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:51:in `render'
  77. /opt/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
  78. /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:51:in `render'
  79. app/controllers/comments_controller.rb:13:in `create'
  80. Request
  81.  
  82. Parameters:
  83.  
  84. {"commit"=>"Post comment",
  85. "post_id"=>"3",
  86. "authenticity_token"=>"2a1e1a9c38c2ea506372602d09929a8c34cedd1f",
  87. "comment"=>{"body"=>"",
  88. "author"=>"",
  89. "email"=>""}}
  90.  
  91. ## Now, after changing the controller to this, here are my results:
  92.  
  93. ## New comments_controller
  94.  
  95. class CommentsController < ApplicationController
  96.  
  97. def create
  98. post_id = params[:post_id]
  99. @post = Post.find(post_id)
  100. @comment = Comment.new(params[:comment])
  101. @comment.post_id = post_id
  102. if @comment.save
  103. # Validations passed
  104. flash[:notice] = "Your comment has been saved, #{@comment.author.titleize}."
  105. redirect_to :controller => "/posts", :id => @post.permalink, :template => "posts/show"
  106. else
  107. render :controller => "/posts", :id => @post.permalink, :template => "posts/show"
  108. end
  109. end
  110.  
  111. def new
  112. @comment = Comment.new
  113. end
  114.  
  115. end
  116.  
  117. ## posts_controller.rb (commented out line 3)
  118.  
  119. def show
  120. @post = Post.find_by_permalink(params[:id])
  121. # @comment = Comment.new
  122. end
  123.  
  124. ## When submitting VALID content for comment:
  125.  
  126. Name can't be blank
  127. Comment can't be blank
  128.  
  129. ## params.inspect on show.html.erb
  130.  
  131. {"commit"=>"Post comment", "authenticity_token"=>"2a1e1a9c38c2ea506372602d09929a8c34cedd1f", "action"=>"create", "post_id"=>"3", "controller"=>"comments", "comment"=>{"body"=>"comment", "author"=>"name", "email"=>"me@me.com"}}
Add Comment
Please, Sign In to add comment