【发布时间】: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