2011-04-18 72 views
0

我最近開始使用Rails進行備份,並且事情一直持續到 。acts_as_commentable驗證

我已經在我的Post模型中設置了acts_as_commentable,它工作得很好。 問題是用戶能夠創建一個「空白」評論。我已在由 acts_as_commentable產生的comment.rb文件 以下驗證來限制評論長度:

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" 

任何想法如何才能呈現一個常規驗證錯誤?感謝 提前!根據要求

UPDATE

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) 
+0

你能告訴你的控制器嗎? – 2011-04-18 18:41:27

+0

添加了評論控制器。希望能幫助到你! – Slythic 2011-04-18 18:53:24

+0

是{{count}}正在工作嗎?你可以檢查development.log瞭解更多細節。你得到的錯誤是因爲對象沒有被保存,並且動作和軌道尋找創建模板來渲染。另外,請嘗試使用%d來代替{{count}}。 – 2011-04-18 19:04:51

回答

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 

希望它可以幫助你以某種方式。

+0

這就是Kleber!我偶然發現了它。我堅持的是在我的帖子show view上的評論表單上渲染驗證錯誤。任何想法如何顯示這些錯誤?對不起,另外的問題! – Slythic 2011-04-18 22:01:34

+0

哦你有沒有嘗試過:渲染'後/顯示'? – 2011-04-19 03:25:21

+0

渲染'posts/show'會顯示錯誤!但是,現在帖子評論並未加載。也許我需要休息一下哈哈。僅供參考我的帖子/ show controller有以下內容:@post = Post.find(params [:id])@comments = @ post.comments @comment = @ post.comments.new#其中@comment是用戶表單評論 – Slythic 2011-04-19 04:18:52