Partial with nested model/controller render back to current post and show errors

Hi all, I get stucked with the following scenario. I have a simple application where I have a post which can have a lot of comments. In the show action of the post I loaded the partial form to create the comment. This is also working. My problem is when the validation for the comment fails I don’t know how to set the render method in the create action of the comments controller to get back to the current post show view and also to display the form errors. I can do a redirect back to the current post but then I will miss all the form data and see also no errors. Hope someone can give me a hint how to solve this.

Here are my models:

class Post < ActiveRecord::Base

extend FriendlyId

friendly_id :title, use: :slugged

attr_accessible :title, :body

attr_accessible :title, :teaser, :content, :category_id, :picture

#paperclip settings

has_attached_file :picture, :styles => { :medium => “300x225>”, :small => “180x115>”, :thumb => “150x150>” }

#association

belongs_to :category

has_many :comments

end

class Comment < ActiveRecord::Base

attr_accessible :content, :email, :name, :website

validates :content, :email, :name, :website, :presence => {:message => “Darf nicht leer sein!”}

belongs_to :post

end

My routes are set like this:

resources :posts do

resources :comments

end

Show view post:

Comments


show here partial with comments

New Comment

<%= render “comments/form” %>

Comments Form Partial:

<%= form_for([@post,@post.comments.build]) do |f| %>

<% if @post.comments.build.errors.any? %>

<%= @post.comments.build.errors.count %> Error while saving form:

    <% @post.comments.build.errors.full_messages.each do |msg| %>

  • <%= msg %>
  • <% end %>

<% end %>

<%= f.label :name %>

<%= f.text_field :name %>

<%= f.label :email %>

<%= f.text_field :email %>

<%= f.label :website%>

<%= f.text_field :website %>

<%= f.label :content %>

<%= f.text_area :content %>

<%= f.submit “Save” %>

<% end %>

create action in 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_path(@post), :notice => “Comment successfully created”) }

else

flash[:alert] = “Something went wrong!”

format.html { render ??? }

end

end

end

Thanks for any help with this.

Cheers Greg

Well, I should have read more on the rails guides website. I found the solution so easy and simple.

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_path(@post), :notice => “Comment successfully created”) }

else

flash[:alert] = “Something went wrong!”

format.html { render ‘posts/show’ }

end

end

end

And also changed this in my partial:

<% if @comment.errors.any? %>

<%= **@comment.errors.count** %> Error while saving form:

    <% @comment.errors.full_messages.each do |msg| %>

  • <%= msg %>
  • <% end %>

<% end %>

Problem solved!

Cheers Greg