Reputation: 111
im not able to display the validation error, even though it is validating the form. In my partial form
seems to be the problem, the @post returns a false for error and so no error is displayed. but the @comment returns a true for error, but im not able to replace @post with @comment since it throws an undefined 'comment' error..
my partial form which is begin called inside the show:
%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
my comments controller:
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
this is show page from which im rendering the file:
%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"
Upvotes: 0
Views: 760
Reputation: 1380
You can use validates_associated on your post model to check for validation errors on your associated model.
Upvotes: 1
Reputation: 712
You have to render new action if @comment.save
fails
if @comment.save
format.html { redirect_to @post }
else
render :action => "new"
end
Upvotes: 0