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

  • Home
  • SEARCH
  • 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 8973163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:23:05+00:00 2026-06-15T18:23:05+00:00

I was just wondering what the recommended approach would be for validating a form

  • 0

I was just wondering what the recommended approach would be for validating a form that is not based on a model. I agree that generally all validation should be done in models but there are situations where a particular form might not have a corresponding model(s). For example, an arbitrary search form.

Based on my current research, there are two main ways of doing it as far as I can see,

  1. Validate in the controller.
    • Is it possible to utilise the Rails validations in the controller? I really would prefer to use them over my own custom code.
  2. Create an arbitrary class eg. called SearchForm and include the ActiveRecord::Validations
    • Should the class be stored with the models (even though it isn’t really)?
    • The posts that recommended this approach indicated you have to stub out a bunch of methods for ActiveRecord such as save, save!, update_attribute, etc. This strikes me as not very Rubyesque at all!
  3. Any other suggestions?
  • 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-06-15T18:23:06+00:00Added an answer on June 15, 2026 at 6:23 pm

    Absolutely #2. ActiveModel::Validations API is what you’re looking for

    class ArbitrarySearch
      include ActiveModel::Validations
    
      attr_accessor :query
    
      validate :query, :presence
    end
    

    As for where this should go, yes, it should go in app/models. If you’re like me and think the mix of models extending ActiveRecord::Base and those that don’t coexisting within the same directory smells funny, consider adding the following to your config/application.rb file

    config.autoload_paths += Dir["#{config.root}/app/models/**/"]
    

    Now, you can organize your model files in whatever way you like. For me I have

    - app
      |
      |- models
         |
         |- table
         |- tableless
         |- observer
    

    and I drop classes like your ArbitrarySearch class into app/models/tableless/arbitrary_search.rb.


    There is a great gem to get this and other common functionality you use within ActiveRecord models into generic non-table-based model classes called active_attr.

    ActiveAttr is a set of modules that makes it easy to create plain old ruby models with functionality found in ORMs, like ActiveRecord, without reinventing the wheel. Think of ActiveAttr as the stuff ActiveModel left out.

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

Sidebar

Related Questions

I was wondering what would be a good approach (or recommended approach) to render
I'm just wondering which one is better, or more recommended for processing form data.
Just wondering what the recommended practice is for importing namespaces. Are you always better
Just wondering is this kind of code recommended to increase performance? void functionCalledLotsofTimes() {
Im just wondering if theres a recommended syntax validator out there anybody can recommend?
I’m just wondering if anyone’s come across or could recommend an IDE that’s capable
I was just wondering when is it recommended to make dll's, when your project
Just wondering if anyone more experienced out there could suggest a recommended limit for
I've just started playing with F#, and was wondering whether there is any recommended
I was just wondering what the best practice would be in this scenario: I

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.