【问题标题】:acts_as_commentable validationsact_as_commentable 验证
【发布时间】:2011-04-18 17:58:42
【问题描述】:

我最近开始使用 Rails,一切都进展顺利,直到 现在。

我在我的 Post 模型中设置了acts_as_commentable,它运行良好。 问题是用户能够创建“空白”评论。我添加了 在由生成的 comment.rb 文件中进行以下验证 act_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"

知道如何仅呈现常规验证错误吗?谢谢在 前进!

更新

CommentsController 根据要求:

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)

【问题讨论】:

  • 你能展示你的控制器吗?
  • 添加了 cmets 控制器。希望对您有所帮助!
  • {{count}} 是否有效?您可以查看 development.log 以获取更多详细信息。你得到的错误是因为对象没有被保存并且动作和rails寻找创建模板来渲染。另外,尝试使用 %d 代替 {{count}}。
  • 不确定 {{count}} 是否有效。我现在只是用“validates_presence_of :comment”替换了验证以保持简单,但我仍然遇到同样的错误。我还编辑了我的帖子以包含错误的日志文件输出。有关如何修复它的任何提示?
  • 卫生署!我刚刚意识到,由于在 CommentsController 中的创建操作上的评论失败时我没有有效的返回,因此显然无法呈现模板。仍然需要找到一种方法来显示验证错误,但向前迈出了一大步。谢谢纳什和纳伦!

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


【解决方案1】:

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

试试这个:

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

希望对你有所帮助。

【讨论】:

  • Kleber 就是这样!我之前偶然发现了它。我坚持的是在我的帖子显示视图的评论表单上呈现验证错误。知道如何显示这些错误吗?很抱歉有额外的问题!
  • render 'posts/show' 确实显示了错误!但是,现在 post cmets 没有加载。也许我需要休息一下哈哈。仅供参考,我的帖子/显示控制器具有以下内容:@post = Post.find(params[:id]) @cmets = @post.cmets @comment = @post.cmets.new #Where @comment 是用户表单评论跨度>
  • 刚刚查看了日志。也许这与它有关?:“Started POST "/posts/1/cmets" for 127.0.0.1" - 不知道为什么要添加 /cmets...
  • 在渲染帖子/节目之前,您可能必须在 create 方法中获取 cmets
  • 克莱伯,你就是男人!做到了 :-) 是否有任何特定的书、文章或课程是您必须学习 Ruby/Rails 的?根据我上面的 cmets,我购买了“使用 Rails 第四版进行敏捷 Web 开发”,应该很快就会收到它,但如果有任何其他可能有帮助的资源,我将不胜感激。感谢您的大力帮助!
猜你喜欢
  • 2014-04-04
  • 1970-01-01
  • 2023-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 2012-05-21
  • 2012-04-18
相关资源
最近更新 更多