Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6251083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:32:52+00:00 2026-05-24T13:32:52+00:00

This might not be the cleanest code yet, still quite new to Ruby… I

  • 0

This might not be the cleanest code yet, still quite new to Ruby…

I have the following spam check method in my message.rb model:

validate :no_spam?, :if => "sender_user_id != nil"

private

def no_spam?
  #first easy spam detection, if the (hidden by css) company field is filled, it is spam for sure
  if !company.blank?
    errors.add(:body, I18n.t(:No_spam_allowed))
    return false
  end

  #Mollom advanced spam detection
  m = Mollom.new(:private_key => 'xxx',
                 :public_key => 'xxx')

  #check content in case it is a first submit of the form
  if captcha_session_id.blank?
    content = m.check_content(:post_body => body,
                              :author_name => sender_name,
                              :author_mail => sender_email,
                              :author_ip => sender_ip)
  else
    #check captcha if the form was resumbitted after an unsure result
    logger.debug "DEBUG: GOING TO CHECK CAPTCHA"
    result = m.valid_captcha?(:session_id => captcha_session_id,
                              :solution => captcha_solution.chomp)
    if result
      logger.debug "DEBUG: TRUE -> GOOD CAPTCHA"
      return true
    else
      logger.debug "DEBUG: FALSE -> BAD CAPTCHA"
      return false
    end
  end

  #returning the right values and error messages for different content check outcomes
  logger.debug "DEBUG: I'M CONTINUING THE METHOD EXECUTION"
  if content.spam?
    logger.debug "DEBUG: SPAM DETECTED"
    errors.add(:body, I18n.t(:No_spam_allowed))
    return false
  elsif content.unsure?
    logger.debug "DEBUG: MESSAGE UNSURE - FAIL FORM BUT SHOW CAPTCHA"
    errors.add(:captcha_solution, I18n.t(:Type_the_characters_you_see_in_the_picture_below))
    self.captcha_image_url = m.image_captcha(:session_id => content.session_id)["url"]
    self.captcha_session_id = content.session_id
    return false
  else
    logger.debug "DEBUG: MESSAGE OK!"
    return true
  end
end

development.log

Processing MessagesController#create (for 127.0.0.1 at 2011-08-12 12:01:24) [POST]
  Parameters: {"commit"=>"Verzend", "action"=>"create", "authenticity_token"=>"xxxxxxxxxxx", "locale"=>"nl", "controller"=>"messages", "message"=>{"sender_email"=>"[FILTERED]", "company"=>"", "body"=>"unsure", "sender_phone"=>"xxxx", "sender_name"=>"Admin ImmoNatie"}}
  Message Columns (6.0ms)   SHOW FIELDS FROM `messages`
  User Columns (10.0ms)   SHOW FIELDS FROM `users`
  User Load (7.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = '1') AND (users.deleted_at IS NULL ) LIMIT 1
  SQL (0.0ms)   BEGIN
  User Update (0.0ms)   UPDATE `users` SET `updated_at` = '2011-08-12 10:01:25', `perishable_token` = 'xxxxxxxxxxx', `last_request_at` = '2011-08-12 10:01:25' WHERE `id` = 1
  SQL (3.0ms)   COMMIT
  SQL (0.0ms)   BEGIN
DEBUG: I'M CONTINUING THE METHOD EXECUTION
DEBUG: MESSAGE UNSURE - FAIL FORM BUT SHOW CAPTCHA
  SQL (0.0ms)   ROLLBACK
Rendering template within layouts/application
Rendering messages/new
Rendered messages/_form (8.0ms)
Rendered layouts/_google_analytics (0.0ms)
Rendered layouts/_login (3.0ms)
Rendered layouts/_navigation (6.0ms)
Rendered layouts/_header (12.0ms)
Rendered about_us/_ten_reasons_9_body (0.0ms)
  NewsletterEmail Columns (5.0ms)   SHOW FIELDS FROM `newsletter_emails`
Rendered layouts/_footer (41.0ms)
Completed in 1907ms (View: 70, DB: 40) | 200 OK [http://infinitize.dynalias.com/contact]
tize.dynalias.com/contact]
  SQL (0.0ms)   SET SQL_AUTO_IS_NULL=0
  Property Columns (10.0ms)   SHOW FIELDS FROM `properties`


Processing MessagesController#create (for 127.0.0.1 at 2011-08-12 12:01:32) [POST]
  Parameters: {"commit"=>"Verzend", "action"=>"create", "authenticity_token"=>"xxxxxxxxxxx", "locale"=>"nl", "controller"=>"messages", "message"=>{"sender_email"=>"[FILTERED]", "company"=>"", "body"=>"unsure", "captcha_solution"=>"", "sender_phone"=>"xx", "captcha_session_id"=>"xxxxxxxxxxx", "sender_name"=>"Admin ImmoNatie"}}
  Message Columns (5.0ms)   SHOW FIELDS FROM `messages`
  User Columns (10.0ms)   SHOW FIELDS FROM `users`
  User Load (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = '1') AND (users.deleted_at IS NULL ) LIMIT 1
  SQL (0.0ms)   BEGIN
  User Update (0.0ms)   UPDATE `users` SET `updated_at` = '2011-08-12 10:01:32', `perishable_token` = 'xxxxxxxxxxx', `last_request_at` = '2011-08-12 10:01:32' WHERE `id` = 1
  SQL (4.0ms)   COMMIT
  SQL (0.0ms)   BEGIN
DEBUG: GOING TO CHECK CAPTCHA
DEBUG: FALSE -> BAD CAPTCHA
  Message Create (0.0ms)   INSERT INTO `messages` (`sender_email`, `receiver_user_id`, `receiver_email`, `created_at`, `body`, `opened_by_owner`, `updated_at`, `receiver_name`, `opened_by_sender`, `sender_ip`, `message_thread_id`, `sender_user_id`, `sender_name`) VALUES('admin@immonatie.be', 1, 'test@immonatie.be', '2011-08-12 10:01:33', 'unsure', NULL, '2011-08-12 10:01:33', 'ImmoNatie', NULL, '127.0.0.1', NULL, 1, 'Admin ImmoNatie')
  MessageThread Columns (5.0ms)   SHOW FIELDS FROM `message_threads`
  MessageThread Create (1.0ms)   INSERT INTO `message_threads` (`last_message_opened_by_sender_id`, `answered`, `initial_sender_user_id`, `created_at`, `initial_sender_email`, `starred`, `updated_at`, `last_message_opened_by_owner_id`, `type_id`, `owner_id`, `property_id`, `first_message_id`, `initial_sender_name`, `initial_sender_phone`, `last_message_id`, `last_message_added_at`) VALUES(213, NULL, 1, '2011-08-12 10:01:33', 'admin@immonatie.be', 0, '2011-08-12 10:01:33', 0, 174, 1, NULL, 213, 'Admin ImmoNatie', 'xx', 213, '2011-08-12 10:01:33')
  Message Update (0.0ms)   UPDATE `messages` SET `updated_at` = '2011-08-12 10:01:33', `message_thread_id` = 101, `sender_name` = 'Admin ImmoNatie', `created_at` = '2011-08-12 10:01:33', `sender_email` = 'admin@immonatie.be', `sender_ip` = '127.0.0.1', `sender_user_id` = 1, `receiver_name` = 'ImmoNatie', `receiver_email` = 'test@immonatie.be', `body` = 'unsure', `receiver_user_id` = 1 WHERE `id` = 213
Sent mail to test@immonatie.be

Date: Fri, 12 Aug 2011 12:01:33 +0200
From: Notifications <no-reply@immonatie.be>
To: test@immonatie.be
Subject: Nieuw contact bericht
Mime-Version: 1.0
Content-Type: text/html; charset=utf-8

Naam: xxxx<br />
E-mail: xxxx<br />
Telefoon: xxxx<br />
<br />
Bericht:<br />
unsure
  SQL (3.0ms)   COMMIT

In the development.log you see that I first submit the form (first create action) with ‘unsure’ in the body to invoke an unsure result from Mollom (this site is in development mode).

The validation fails for this first create action as expected.

But when I resubmit the form now with an empty captcha verification code, the validation method returns false, but the validation does not, since the save is continued and COMMITTED in the end.

Why does this validation not fail and break (rollback) the create action?

Thanks,
Michael

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:32:52+00:00Added an answer on May 24, 2026 at 1:32 pm

    Found the problem. Returning false to the validation was not enough. I needed to add an error: errors.add(:body, “error message”). This error together with return false did the job.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might not have a major usecase in projects, but I was just trying
This might not appear like a programming question, but inherently deals with code. I
Technically this might not be classed as a programming question, since I have already
This might not be quite the question you're expecting! I don't want a regex
This might not be completely relevant to stackoverflow but I have no better site
Reading the documentation it seems this might not be possible, but it seems that
This question might not seem programming related at first, but let me explain. I'm
This might be iPhone specific, I'm not sure. The compiler doesn't complain when building
This might be an easy one, but I'm not really experienced with sql. I
This might be a stupid question, but I was wondering whether or not you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.