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 8627993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:25:18+00:00 2026-06-12T08:25:18+00:00

I have two fields in a form I would like to validate the presence

  • 0

I have two fields in a form I would like to validate the presence of, before sending. The problem though is that the controller’s model doesn’t have these fields in the database, so I tried making virtual attributes. I’m not quite sure how to get it to work though.

I tried doing this in the Model, called “Find_number”

class FindNumber < ActiveRecord::Base

    attr_accessor :name
    attr_accessor :original_number

    validates :name, presence: true
    validates :original_number, presence: true

end

and the following in the create action of the Find_numbers controller

def create
    @user = current_user
    client = Twilio::REST::Client.new(@user.twilio_account_sid, @user.twilio_auth_token)

    search_params = {}
      %w[in_postal_code near_number contains].each do |p|
        search_params[p] = params[p] unless params[p].nil? || params[p].empty?
      end

    local_numbers = client.account.available_phone_numbers.get('US').local
    @numbers = local_numbers.list(search_params)

    if :name.valid? && :original_number.errors.any?
        unless @numbers.empty?
        render 'find_numbers/show'
        else
        flash.now[:error] = "Sorry, We Couldn't Find Any Numbers That Matched Your Search! Maybe Something Simpler?"    
        render 'find_numbers/new'
        end
    else
    flash.now[:error] = "Sorry, We Couldn't Find Any Numbers That Matched Your Search! Maybe Something Simpler?"    
    render 'find_numbers/new'   
    end     
end

When I enter info though, I get the error

undefined method `valid?' for :name:Symbol

I’m probably calling the :name and :original_number attributes incorrectly, and the double if then statements look very newbish :P.

What would I need to replace if :name.valid? && :original_number.errors.any? , to make sure that it validates? Or is there a lot more I’m missing?

  • 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-12T08:25:20+00:00Added an answer on June 12, 2026 at 8:25 am

    I think you are confusing the boundary between ‘controller’ and ‘model’. The controller doesn’t know anything about the validations inside of the model that you’ve written. The fact that the controller is called FindNumbersController and the model is called FindNumber doesn’t really mean anything in terms of shared functionality.

    You would need to explicitly create an instance of the model with the passed in params, and let the model perform the validation on the instance

    find_number = FindNumber.new(params.slice(:name, :original_number))
    

    Then you can ask whether the instance as a whole is valid

    find_number.valid?
    

    or whether a specific field has any error messages

    find_number.errors[:field].any?
    

    So, :name.valid? becomes find_number.errors[:name].empty? and :original_number.errors.any? becomes find_number.errors[:original_number].any?

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

Sidebar

Related Questions

I have two form fields and would like to post those values using jquery
Imagine that I have a form in a flash application with two fields, input1
I have two pieces of javascript that I would like to work together. I
I'm using this two plug-ins jquery.validate and jquery.form separately, but I would like to
I have two form fields on my view page, a date input (with a
I have two fields on the form ( forgotpassword form ) username and email
I have two select fields in a form. Every time one of these select
Say I have two fields in a new or edit form: <%= f.text_field :email
I have a form with (at least) the following two fields: country club The
I have a usual login form consisting of two input fields, one for login,

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.