Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## posts / show.html.erb
- <h1><%= h @post.title %></h1>
- <div class="post">
- <p class="created_at">Posted <%= pretty_datetime(@post.created_at) %></p>
- <p><%= sanitize @post.body %></p>
- </div>
- <% unless @post.comments.empty? -%>
- <div class="comments">
- <h2>Comments</h2>
- <%= render :partial => @post.comments %>
- </div>
- <% end -%>
- <div class="comment_form">
- <h2>Post a comment</h2>
- <%= error_messages_for :comment %>
- <% form_for :comment, :url => post_comments_path(@post.id) do |f| %>
- <div>Name</div>
- <div><%= f.text_field :author %></div>
- <div>Email <span class="small gray">— optional, shown to public</span></div>
- <div><%= f.text_field :email %></div>
- <div>Comment</div>
- <div><%= f.text_area :body, :size => "30x6" %></div>
- <div><%= submit_tag "Post comment" %></div>
- </div>
- <% end -%>
- ## comments_controller.rb
- class CommentsController < ApplicationController
- def create
- post_id = params[:post_id]
- @post = Post.find(post_id)
- @comment = Comment.new(params[:comment])
- @comment.post_id = post_id
- if @comment.save
- # Validations passed
- flash[:notice] = "Your comment has been saved, #{@comment.author.titleize}."
- redirect_to post_path(@post.permalink)
- else
- render post_path(@post.permalink)
- end
- end
- def new
- @comment = Comment.new
- end
- end
- ## comment.rb
- class Comment < ActiveRecord::Base
- validates_presence_of :name, :comment
- validates_format_of :email, :with => /\A\w+\@\w+\.\w+\Z/, :unless => :email_blank?
- belongs_to :post, :counter_cache => true # comments.post_id
- def email_blank?
- self.email.blank?
- end
- end
- ## Error when submitting blank comment
- ActionController::RenderError in CommentsController#create
- You called render with invalid options : /posts/nullam-tincidunt
- RAILS_ROOT: /Users/kip/svn/doohickey
- Application Trace | Framework Trace | Full Trace
- /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:841:in `render_with_no_layout'
- /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/layout.rb:262:in `render_without_benchmark'
- /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:51:in `render'
- /opt/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
- /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/benchmarking.rb:51:in `render'
- app/controllers/comments_controller.rb:13:in `create'
- Request
- Parameters:
- {"commit"=>"Post comment",
- "post_id"=>"3",
- "authenticity_token"=>"2a1e1a9c38c2ea506372602d09929a8c34cedd1f",
- "comment"=>{"body"=>"",
- "author"=>"",
- "email"=>""}}
- ## Now, after changing the controller to this, here are my results:
- ## New comments_controller
- class CommentsController < ApplicationController
- def create
- post_id = params[:post_id]
- @post = Post.find(post_id)
- @comment = Comment.new(params[:comment])
- @comment.post_id = post_id
- if @comment.save
- # Validations passed
- flash[:notice] = "Your comment has been saved, #{@comment.author.titleize}."
- redirect_to :controller => "/posts", :id => @post.permalink, :template => "posts/show"
- else
- render :controller => "/posts", :id => @post.permalink, :template => "posts/show"
- end
- end
- def new
- @comment = Comment.new
- end
- end
- ## posts_controller.rb (commented out line 3)
- def show
- @post = Post.find_by_permalink(params[:id])
- # @comment = Comment.new
- end
- ## When submitting VALID content for comment:
- Name can't be blank
- Comment can't be blank
- ## params.inspect on show.html.erb
- {"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