开发者

acts_as_commentable validations

开发者 https://www.devze.com 2023-02-26 06:02 出处:网络
I recently started back up with Rails and things are going well up until now. I\'ve set up acts_as_commentable in my Post model and it\'s working great.

I recently started back up with Rails and things are going well up until now.

I've set up acts_as_commentable in my Post model and it's working great. Problem is users are able to create a "blank" comment. I've added the following validations in the comment.rb file generated by acts_as_commentable to limit the comment length:

validates_length_of :comment, :minimum => 3, :too_short => "must be at
least {{count}} words.", :tokenizer => lambda {|str| str.scan(/\w+/) }

validates_length_of :comment, :maximum => 200, :too_long => "must be
shorter than {{count}} words. Make sure there are no links or
elements.", :tokenizer => lambda {|str| str.scan(/\w+/) }

The show view form for the comment is the following:

<%- form_for [@post, @comment] do |f|-%>
  <%= f.error_messages %>
  <%= f.text_area :comment, :rows => 3 -%>
  <p><%= f.submit -%></p>
<%- end -%>

However I am getting the following error only when validation fails (if a normal length comment is created the site works):

Template is missing

Missing template comments/create with {:handlers=>[:erb, :rjs, :builder,
:rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths
"/...", "/.../.rvm/gems/ruby-1.9.2-head/gems/devise-1.2.1/app/views"

Any idea how I can render just a regular validation error? Thanks in advance!

UPDATE

CommentsController as requested:

class CommentsController < ApplicationController
  before_filter :authenticate_user!

  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new(params[:comment])
    @comment.user_id = current_user.id
    if @comment.save
      redirect_to @post
    end
  end

  def destroy
    session[:return_to] ||= request.referer

    if current_user.admin?
      @comment = Comment.find(params[:id])
    else
      @comment = current_user.comments.find(params[:id])
    end

    @comment.destroy

    respond_to do |format|
      format.html { redirect_to session[:return_to] }
      format.xml  { head :ok }
    end
  end

end

Development Log:

Started POST "/posts/1/comments" for 127.0.0.1 at 2011-04-18 15:20:05 -0400
  Processing by CommentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"P5x6xSS6VgxPg58Ubftc4FcUQKZFQbpKOKO9zFeE7cM=", "comment"=>{"comment"=>""}, "commit"=>"Create Comment", "post_id"=>"1"}
  User Load (0.7ms)  SELECT "users".* FROM "users" 开发者_开发技巧WHERE "users"."id" = 1 LIMIT 1
  SQL (0.3ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'

  Post Load (0.4ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = 1 LIMIT 1
Completed   in 154ms

ActionView::MissingTemplate (Missing template comments/create with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/.../app/views", "/.../vendor/plugins/dynamic_form/app/views", "/.../.rvm/gems/ruby-1.9.2-head/gems/devise-1.2.1/app/views"):


Rendered /.../.rvm/gems/ruby-1.9.2-head/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.8ms)


This happening because you haven't defined yet what controller should do if validation fails.

Try this:

def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.new(params[:comment])
  @comment.user_id = current_user.id
  if @comment.save
    redirect_to @post
  else # validation fails
    render 'new'
  end
end

Hope it helps you somehow.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号