I’m current following the Rails Getting started guide, including creating a blog with post and comments models. In the post show method, there is a form to create a new comment. Like so.
<%= form_for([@post, @post.comments.build]) do |f| %>
<div class="field">
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
However, I want to prohibit users from posting more than one comment on a particular post. How would I go about doing this? I’ve been working with validators but I can’t seem to wrap my head around adding errors and displaying them on the posts#show page.
The issue was actually in my controller. Since
submissionsare a nested resource ofposts, I need to re-set@postbefore callingrender 'posts/show'in case of an error. This works, however you’ll notice that you’ll end up on a URL such as/posts/4/submissions/with the appropriate webpage, rather than/posts/4