title: How to render validation errors of form partial - Rails
body: <p>I am building a custom blog. I have <code>posts</code> and <code>comments</code>.</p>
  
  <p>I render comments through a partial on the show action of individual posts.</p>
  
  <p><strong>posts controller</strong></p>
  
  <pre><code> class Blog::PostsController &lt; Blog::BaseController
  
    def show
      @post = Post.find_by_permalink(params[:id])
      @comment = Comment.new(:post =&gt; @post)
    end
  
  end
  </code></pre>
  
  <p><strong>comments controller</strong></p>
  
  <pre><code>class Blog::CommentsController &lt; Blog::BaseController
  
    def create
      @comment = Comment.new(comment_params)
      if @comment.save
        flash[:success] = "Comment successfully created!"
        redirect_to blog_post_url(@comment.post)
      else
        flash[:warning] = "Something went wrong, try again. If problem persists please let our team know about it!"
        redirect_to :back
      end
    end
  
    private
        def comment_params
          params.require(:comment).permit(:body,:name,:email,:user_id,:post_id)
        end
  end
  </code></pre>
  
  <p><strong>show.html.erb</strong></p>
  
  <pre><code>&lt;div class="row post-container"&gt;
    &lt;div class="large-offset-1 large-7 medium-12 columns post-content"&gt;
      &lt;h1 class="post-title"&gt; &lt;%= link_to @post.title, blog_post_path(@post) %&gt; &lt;/h1&gt;
      &lt;p class="published-date"&gt;&lt;em&gt;Published on &lt;%= l @post.published_at, format: :date %&gt;&lt;/em&gt;&lt;/p&gt;
  
      &lt;div class="post-body"&gt;
        &lt;%= @post.body.html_safe %&gt;
        &lt;%= render partial: "blog/comments/comment", post: @post %&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="large-4 columns sidebar"&gt;
      sidebar
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>comments partial form</strong></p>
  
  <pre><code>&lt;%= form_for [:blog,@comment] do |f| %&gt;
    &lt;%= render 'blog/shared/error_messages', object: f.object %&gt;
  
    &lt;div class="field panel"&gt;
      &lt;%= f.label :name %&gt;&lt;br&gt;
      &lt;%= f.text_field :name,class: 'form-control' %&gt;
    &lt;/div&gt;
  
    &lt;div class="field panel"&gt;
      &lt;%= f.label :email %&gt;&lt;br&gt;
      &lt;%= f.text_field :email,class: 'form-control' %&gt;
    &lt;/div&gt;
  
    &lt;div class="field panel"&gt;
      &lt;%= f.label :body, "Comment" %&gt;&lt;br&gt;
      &lt;%= f.text_area :body,class: 'form-control' %&gt;
    &lt;/div&gt;
  
    &lt;% if logged_in? %&gt;
      &lt;%= f.hidden_field :user_id, value: current_user.id %&gt;
    &lt;% end %&gt;
  
    &lt;%= f.hidden_field :post_id %&gt;
  
  
  
    &lt;div class="actions"&gt;
      &lt;%= f.submit "Create comment", class:"btn btn-danger btn-block" %&gt;
      &lt;a class="btn btn-warning btn-block cancel"&gt;Cancel&lt;/a&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>error messages partial</strong></p>
  
  <pre><code>&lt;% if object.errors.any? %&gt;
    &lt;div id="error_explanation"&gt;
      &lt;div class="errors-alert text-center"&gt;
        The form contains &lt;%= pluralize(object.errors.count, "error") %&gt;.
      &lt;/div&gt;
      &lt;ul&gt;
      &lt;% object.errors.full_messages.each do |msg| %&gt;
        &lt;li class="errors-alert-item text-center"&gt;&lt;%= msg %&gt;&lt;/li&gt;
      &lt;% end %&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I tried to put validations on the comments model but it does not display them even though it correctly redirects back.</p>
  
  <p>I know I have to use render instead of redirect but I do not know what to render. This is what I am trying to figure out since I do not have a render new action.</p>