ходовые рельсы 3.2.1.
Я ознакомился с руководством по началу работы на сайте Ruby on Rails. Я создал сообщение в блоге, где кто-то может комментировать сообщения.
Я изменил пример так, чтобы при вводе комментариев, которые не проверяются (нет имени или текста комментария), отображается ошибка.
(пост содержит несколько комментариев и т. Д.)
Однако, как я могу, чтобы Rails поместил этот проблемный комментарий обратно в форму, а не на страницу?
Вот мой метод создания в контроллере комментариев:
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, :notice => 'Comment was successfully created.') }
else
format.html { render 'posts/show' }
end
end
конец
Вот мой show.html.erb из сообщений:
<%= render @post %>
<h3>Comments</h3>
<%= render @post.comments %>
<h3>Add a comment</h3>
<%= render "comments/form" %>
<p>
<% if user_signed_in? %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<% end %>
<%= link_to 'Back', posts_path %>
</p>
А вот мой частичный комментарий:
<%= form_for([@post, @post.comments.build]) do |f| %>
<%= render "shared/error_messages", :target => @comment %>
<div class="field">
<%= f.label "Name" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit "Add Comment!" %>
</div>
<% end %>
А вот мой частичный комментарий:
<p>
<strong><%= comment.name %></strong><br />
<%= comment.created_at.try(:strftime, "on %A, %B %d %Y at %H:%M:%S") %><br />
<%= simple_format(h(comment.body), :sanitize => true) %>
</p>