Я делаю простой блог на rails. У меня есть модель Post и модель Comment. При создании комментария, если комментарий не валидный, я хочу показать ошибку. Как мне сделать?
модель Post:
#/models/post.rb
class Post < ActiveRecord::Base
has_many :comments
validates :title, :content, :presence => true
end
модель Comment:
#/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
validates :name, :comment, :presence => true
end
Контроллер Comments
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
redirect_to post_path(@post)
end
end
Вид для формы комментария:
<%= form_for([@post, @post.comments.build]) do |f| %>
<% if @comment.errors.any? %>
error!
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :comment %><br />
<%= f.text_area :comment %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= render 'comments/form' %>
Как передать @comment из контроллера CommentController в вид /post/show.html.erb ?
Спасибо заранее.