简体   繁体   中英

Rails Form - Check for pre-existing data and populate fields

I'm working on a rails app that allows field agents to report ticket(s) on behalf of clients. I am capturing the data using a model called "submission" where the agents can input the client's details and submit one or more tickets for the client.

I want to ensure that if a client already exists then a new record is not created for the same client. I would also like to ensure that if a client has a name similar to a pre-existing record then the clients don't get mixed up eg: 2 people named John Smith.

My current submission form:

<%= form_for(@submission) do |f| %>

  # some submission fields here ...

  <h4>Client Details</h4>
  <%= f.fields_for :client do |c| %>
    <div class="row">
      <div class="field col-xs-6">
        <%= c.label :first_name %><br>
        <%= c.text_field :first_name, class: 'form-control' %>
      </div>
      <div class="field col-xs-6">
        <%= c.label :last_name %><br>
        <%= c.text_field :last_name, class: 'form-control' %>
      </div>
    </div>

   # more client fields....

  <% end %>

<% end %>

How can I add a function to check for existing records that match the first and last names when you fill in the fields? Also, how can I ensure that if 2 clients exist with the same name then this can be identified? Could I maybe use the phone number as a unique identifier?

I know my question is a bit vague but I am unsure of how best to tackle this issue (autocomplete? maybe a checkbox that has options for new or existing client?)

Any help would be most appreciated.

You probably want to use Rails validations to ensure the first name and last name of client will not repeat. For example:

class Client < ActiveRecord::Base
  validates :first_name, uniqueness: true
  validates :last_name, uniqueness: true
end

But this will reject any that has same first name but different last name or the other case. In this situation you might search for custom validation method:

class Client < ActiveRecord::Base
  validate :same_full_name

  def same_full_name
    # or different logic/additional condition
    if Client.exists?(first_name: first_name, last_name: last_name, phone_number: phone_number)
      errors.add("Client with this name exists.")
    end
  end
end

This will prompts error when user tries to submit the form. When Rails validation failed they will be redirected to this page(assuming that standard REST actions applied) and see the error message(if there's any flash or error message in your view).

If instant error prompt is preferred, you may want a JavaScript client integration with Rails validation such as client_side_validations gem, or use jquery validation for pure Javascript validation.

For the problem stated in the comment:

If a client does already exist in the database, how can I populate the form with this data and save the submission with the same client?

This depends on the web flow, another of the solution might be using ajax to call a controller action to check whether the client exists. If it does then you can use jQuery to populate the returned client data(from the controller) to the form, instead of doing the validation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM