Ruby on rails 充当可注释的验证
我最近开始使用Rails进行备份,直到 现在 我已经在我的帖子模型中设置了acts_as_commentable,效果很好。 问题是用户能够创建“空白”注释。我添加了 在由生成的comment.rb文件中执行以下验证: 作为可注释的,限制注释长度:Ruby on rails 充当可注释的验证,ruby-on-rails,validation,acts-as-commentable,Ruby On Rails,Validation,Acts As Commentable,我最近开始使用Rails进行备份,直到 现在 我已经在我的帖子模型中设置了acts_as_commentable,效果很好。 问题是用户能够创建“空白”注释。我添加了 在由生成的comment.rb文件中执行以下验证: 作为可注释的,限制注释长度: validates_length_of :comment, :minimum => 3, :too_short => "must be at least {{count}} words.", :tokenizer => lambda
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 -%>
你知道我怎样才能呈现一个常规的验证错误吗?谢谢你
前进
更新
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
发生这种情况是因为您还没有定义如果验证失败控制器应该做什么 试试这个:
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
希望它能对您有所帮助。您能展示控制器吗?添加了控制器评论。希望有帮助!{{count}}在工作吗?您可以查看development.log以了解更多详细信息。由于对象未保存且操作和rails查找要渲染的创建模板,因此出现错误。另外,尝试使用%d代替{{count}。不确定{count}是否工作。现在我只是用“validates\u presence\u of:comment”替换了验证,以保持简单,我仍然会遇到同样的错误。我还编辑了我的帖子,以包含错误的日志文件输出。有关于如何修复它的提示吗?啊!我刚刚意识到,由于当CommentsController中的创建操作失败时,我没有有效的返回,因此它显然无法呈现模板。仍然需要找到一种方法来显示验证错误,但这是向前迈出的一大步。谢谢纳什和纳伦!克莱伯就是这样!我刚才偶然发现的。我遇到的问题是在我的posts show视图的评论表单上呈现验证错误。知道如何显示这些错误吗?很抱歉,还有一个问题!渲染“posts/show”确实显示错误!但是,现在没有加载帖子评论。也许我需要休息一下哈哈。仅供参考,my posts/show controller具有以下功能:@post=post.find(params[:id])@comments=@post.comments@comments=@post.comments.new#其中@comment是刚刚在日志中查看的用户表单comments。也许这与此有关?:“Started POST”/posts/1/comments”for 127.0.0.1”-不确定为什么要添加/comments…也许你必须在呈现帖子/showKleber之前,在create方法上获取评论,你就是那个人!确实如此:-)有没有任何特定的书籍、文章或课程需要学习Ruby/Rails?根据我上面的评论,我购买了“使用Rails的敏捷Web开发第四版”,应该很快就会收到,但如果有其他可能的帮助,我将不胜感激。谢谢你的帮助!
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)
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