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! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 18 April 2011 18:22, Tony Tony <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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]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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #993612:> On 18 April 2011 18:22, Tony Tony <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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): >> >> [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? > > ColinHi 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! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.