# acts\_as\_commentable validations

**URL:** <https://discuss.rubyonrails.org/t/acts-as-commentable-validations/58516>\
**Category:** rubyonrails-talk\
**Created:** [April 18, 2011, 5:22pm UTC](https://discuss.rubyonrails.org/t/acts-as-commentable-validations/58516 "2011-04-18T17:22:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 18, 2011, 5:22pm UTC](https://discuss.rubyonrails.org/t/acts-as-commentable-validations/58516/1 "2011-04-18T17:22:09Z")

</div>

Hi all,

I recently started back up with Rails and things are going well up until now.

I've set up acts\_as\_commentable in my Post model and it's working great. Problem is users are able to create a "blank" comment. I've added the following validations in the comment.rb file generated by acts\_as\_commentable to limit the comment length:

[code] validates\_length\_of :comment, :minimum =\> 3, :too\_short =\> "must be at least {{count}} words.", :tokenizer =\> lambda {|str| str.scan(/\w+/) }

validates\_length\_of :comment, :maximum =\> 200, :too\_long =\> "must be shorter than {{count}} words. Make sure there are no links or elements.", :tokenizer =\> lambda {|str| str.scan(/\w+/) } [/code]

The show view form for the comment is the following:

[code] \<%- form\_for [@post, @comment] do |f|-%\> &nbsp;&nbsp;\<%= f.error\_messages %\> &nbsp;&nbsp;\<%= f.text\_area :comment, :rows =\> 3 -%\> &nbsp;&nbsp;\<p\>\<%= f.submit -%\>\</p\> \<%- end -%\> [/code]

However I am getting the following error only when validation fails (if a normal length comment is created the site works):

[code] Template is missing

Missing template comments/create with {:handlers=\>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=\>[:html], :locale=\>[:en, :en]} in view paths "/...", "/.../.rvm/gems/ruby-1.9.2-head/gems/devise-1.2.1/app/views" [/code]

Any idea how I can render just a regular validation error? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [April 18, 2011, 7:58pm UTC](https://discuss.rubyonrails.org/t/acts-as-commentable-validations/58516/2 "2011-04-18T19:58:46Z")

</div>

What does the code for action create in the controller look like? What happens in there if the validation fails (ie save will return zero). What view does it attempt to show in that case?

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 18, 2011, 8:13pm UTC](https://discuss.rubyonrails.org/t/acts-as-commentable-validations/58516/3 "2011-04-18T20:13:01Z")

</div>

Colin Law wrote in post #993612:

> > The show view form for the comment is the following: However I am getting the following error only when validation fails (if a normal length comment is created the site works):
> > 
> > [code] Template is missing
> > 
> > Missing template comments/create with {:handlers=\>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=\>[:html], :locale=\>[:en, :en]} in view paths "/...", "/.../.rvm/gems/ruby-1.9.2-head/gems/devise-1.2.1/app/views" [/code]
> 
> What does the code for action create in the controller look like? What happens in there if the validation fails (ie save will return zero). What view does it attempt to show in that case?
> 
> Colin

Hi Colin,

Ok so I just figured out the main problem was with the create action in CommentsController.

I had:

&nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@post = Post.find(params[:post\_id]) &nbsp;&nbsp;&nbsp;&nbsp;@comment = @post.comments.new(params[:comment]) &nbsp;&nbsp;&nbsp;&nbsp;@comment.user\_id = current\_user.id &nbsp;&nbsp;&nbsp;&nbsp;if @comment.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to @post &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

What I needed was to add an else statement to @comment.save like so:

&nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@post = Post.find(params[:post\_id]) &nbsp;&nbsp;&nbsp;&nbsp;@comment = @post.comments.new(params[:comment]) &nbsp;&nbsp;&nbsp;&nbsp;@comment.user\_id = current\_user.id &nbsp;&nbsp;&nbsp;&nbsp;if @comment.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to @post &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to @post &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

Now the problem I'm having showing the validation errors on my post show form (code in my original message). Any tips on displaying that? Thanks!
