title: Pass a variable from controller to view
body: <p>I do a simple blog on rails.
  I have a Post model and a Comment model.
  When you create a comment, if comment is not valid, i want to show the error.
  How do I do?</p>
  <p>model Post:</p>
  <pre><code>#/models/post.rb 
  class Post &lt; ActiveRecord::Base
     has_many :comments
     validates :title, :content, :presence =&gt; true
  end
  </code></pre>
  <p>model Comment:</p>
  <pre><code>#/models/comment.rb
  class Comment &lt; ActiveRecord::Base
     belongs_to :post
     validates :name, :comment, :presence =&gt; true
  end
  </code></pre>
  <p>Comments Controller</p>
  <pre><code>class CommentsController &lt; ApplicationController
    def create
      @post = Post.find(params[:post_id])
      @comment = @post.comments.create(params[:comment])
      redirect_to post_path(@post)
    end
  end
  </code></pre>
  <p>View for comment form:</p>
  <h1>/views/comments/_form.html.erb</h1>
  <pre><code>&lt;%= form_for([@post, @post.comments.build]) do |f| %&gt;
    &lt;% if @comment.errors.any?  %&gt;
       error! 
    &lt;% end %&gt;
    &lt;div class=&quot;field&quot;&gt;
      &lt;%= f.label :name %&gt;&lt;br /&gt;
      &lt;%= f.text_field :name %&gt;
    &lt;/div&gt;
    &lt;div class=&quot;field&quot;&gt;
      &lt;%= f.label :comment %&gt;&lt;br /&gt;
      &lt;%= f.text_area :comment %&gt;
    &lt;/div&gt;
    &lt;div class=&quot;actions&quot;&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  <h1>/views/posts/show.html.erb</h1>
  <pre><code>&lt;%= render 'comments/form' %&gt;
  </code></pre>
  <p>How to pass @comment from the controller CommentController to view /post/show.html.erb ?</p>
  <p>Thanks in advance.</p>