невозможно отобразить ошибку проверки из частичной формы - PullRequest
0 голосов
/ 28 февраля 2012

Я не могу отобразить ошибку проверки, даже если она проверяет форму. В моей частичной форме

  • if @ post.errors.any?

, похоже, проблема, @post возвращает false для ошибки, поэтому ошибка не отображается. но @comment возвращает true для ошибки, но я не могу заменить @post на @comment, так как он выдает неопределенную ошибку 'comment' ..

моя частичная форма, которая начинается внутри шоу:

%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"

Ответы [ 2 ]

1 голос
/ 28 февраля 2012

Вы можете использовать validates_associated в вашей модели постов, чтобы проверить ошибки валидации в вашей связанной модели.

0 голосов
/ 28 февраля 2012

Вы должны выполнить новое действие, если @comment.save не удается

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