2012-02-28 86 views
0

即使驗證窗體,我也無法顯示驗證錯誤。 以我的部分形式無法顯示來自部分表單的驗證錯誤

  • if @ post.errors.any?

似乎是問題,@post返回錯誤的錯誤,因此沒有錯誤顯示。 但@comment返回一個真正的錯誤,但我不是能夠與@comment更換@post因爲它拋出一個不確定的「註釋」的錯誤..

我的部分形成被稱爲開始顯示內:

%html 
%head 
%title= @post.title 
    %body 
    %h2 Create Comments: 
    = form_for([@post, @post.comments.build]) do |f| 
     - if @post.errors.any?    # <--- Unsure what to do here.. 
     #error_explanation 
     %h2 
      = pluralize(@post.errors.count, "error") 
      prohibited this cart from being saved: 
     %ul 
      - @post.errors.full_messages.each do |msg| 
      %li= msg 
    = f.label :Name 
    %br 
    = f.text_field :name 
    %br 
    %br 
    = f.label :Text 
    %br 
    = f.text_area :text 
    %br 
    %br 
    = f.submit 

我的意見控制器:

def create 
@post = Post.find(params[:post_id]) 

@comment = @post.comments.build(params[:comment]) 
    respond_to do |format| 
if @comment.save 
    format.html { redirect_to @post } 
else 
    format.html { redirect_to @post } 
end 
    end 
end 

這是表演頁從即時通訊渲染文件:

%html 
%head 
    %title= @post.title 
%body 
    %strong Author name: 
    = @post.author_name 
    %br 
    %br 
    %strong Title: 
    = @post.title 
    %br 
    %br 
    %strong Email: 
    = @post.email 
    %br 
    %br 
    %strong Description: 
    = @post.description 

    %h2 Comments for this Post: 

    = render :partial => @post.comments 

    = render :partial => "comments/form" 

回答

1

您可以使用validates_associated在您的文章模型檢查驗證錯誤您關聯的模型。

+0

thanx答覆..驗證工作正常,但即時通訊不能顯示錯誤裏面的條件,如果條件@post失敗的條件,並出來.. – 2012-02-28 13:30:56

+0

如果您使用validates_associated,@ post.errors將返回錯誤爲評論,並且你可以很容易地顯示他們.... – shuriu 2012-02-28 13:53:55

+0

我做了它,但它stil does not給我的錯誤消息..當我調試代碼我沒有@post內的錯誤消息.. – 2012-02-28 14:16:29

0

你必須使新的行動,如果失敗@comment.save

if @comment.save 
    format.html { redirect_to @post } 
else 
    render :action => "new" 
end 
相關問題