acts_as_commentable验证

时间:2011-04-18 17:58:42

标签: ruby-on-rails validation acts-as-commentable

我最近开始使用Rails,事情进展顺利,直到 现在

我在Post模型中设置了acts_as_commentable,它运行良好。 问题是用户能够创建“空白”评论。我添加了 在生成的comment.rb文件中进行以下验证 acts_as_commentable限制评论长度:

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+/) }

评论的节目视图表格如下:

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

但是,只有在验证失败时才会收到以下错误(如果 正常长度评论创建网站工作):

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"

知道如何只渲染常规验证错误吗?谢谢 前进!<​​/ P>

更新

按要求发表评论:

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

开发日志:

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)

1 个答案:

答案 0 :(得分:2)

发生这种情况是因为如果验证失败,您还没有定义控制器应该做什么。

试试这个:

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

希望它能以某种方式帮助你。