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:

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+/) }

The show view form for the comment is the following:

<%- form_for [@post, @comment] do |f|-%>
  <%= f.error_messages %>
  <%= f.text_area :comment, :rows => 3 -%>
  <p><%= f.submit -%></p>
<%- end -%>

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

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"

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

On 18 April 2011 18:22, Tony T. [email protected] wrote:

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):

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"

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 L. wrote in post #993612:

On 18 April 2011 18:22, Tony T. [email protected] wrote:

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):

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"

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!