acts_as_commentable validations

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|-%>   <%= f.error_messages %>   <%= f.text_area :comment, :rows => 3 -%>   <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!

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

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:

  def create     @post = Post.find(params[:post_id])     @comment = @post.comments.new(params[:comment])     @comment.user_id = current_user.id     if @comment.save       redirect_to @post     end   end

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

  def create     @post = Post.find(params[:post_id])     @comment = @post.comments.new(params[:comment])     @comment.user_id = current_user.id     if @comment.save       redirect_to @post     else       redirect_to @post     end   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!