2

I have a search form and phone number verification form i would like know how associate the forms, i want display the search results only after the phone validation and the user fills out only one form with product criteria and his phone number for the validation,

My search form

<%= form_tag recherche_path, id: "address_input", method: :get, autocomplete: 'on' do %>
          <%= text_field_tag :location, params[:location], {id: 'address', class: "namanyay-search-box", placeholder: 'Adresse du bien à vendre', :required => true} %>
          <%= text_field_tag :room_type, params[:room_type], {class: "namanyay-search-box-type", :required => true}  %>
          <%= submit_tag "Trouver une agence", id: "namanyay-search-btn" %>
        <% end %>

My phone number validation form

<%= form_for PhoneNumber.new, remote: true do |f| %>
            <div class="form-group">
              <%= f.text_field :phone_number, class: "namanyay-search-box" %>
            </div>
            <%= f.submit "Envoyer", class: "btn btn-danger", id: 'send-pin-link' %>
          <% end %>
        </div>

        <div id="verify-pin">
        <%= form_tag phone_numbers_verify_path, remote: true do |f| %>
          <%= hidden_field_tag 'hidden_phone_number', '' %>
          <div class="form-group">
            <%= text_field_tag :pin %>
          </div>
          <%= submit_tag "Verifier", class: "btn btn-danger" %>
          <% end %>

I dont know how make it work in my controller

Antoine Wako
  • 145
  • 1
  • 17

1 Answers1

2

Assuming you're using the twilio-ruby gem. If you don't have a Twilio controller yet, make one like this:

class TwilioController < ApplicationController

  def lookup
    respond_to do |format|
      if valid_phone_number(twilio_params)
        format.json { head :ok }
      else
        format.json { render json: 'error', status: :unprocessable_entity }
      end
    end
  end

  private

  def valid_phone_number?(phone_number)
    client = Twilio::REST::Client.new(ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN'])
    begin
      response = client.lookups.phone_numbers(phone_number).fetch
      response.phone_number #if invalid, throws an exception. If valid, no problems.
      return true
    rescue => e
      if e.code == 20404
        return false
      else
        raise e
    end
  end

  def twilio_params
    params.require(:twilio_number)
  end
end

Add a route:

post '/twilio/lookup', to: 'twilio#lookup'

All you have to do then is add some Javascript to send a POST request to that route with the parameter twilio_number. (see this post if you need help doing that: How to pass parameters in $ajax POST?)

You might have to tweak the JSON responses in the lookup action to suit your needs, but this should do the trick as far as validating the phone numbers.

NM Pennypacker
  • 6,704
  • 11
  • 36
  • 38
  • Twilio evangelist here. Thanks for the answer Nick! Can I just point out that the latest version of the Twilio gem no longer has a `Twilio::REST::LookupsClient` it's all done through one `Twilio::REST::Client` now and the call is to `client.lookups.phone_numbers(phone_number).fetch` instead. Everything else should work the same though. – philnash Sep 28 '17 at 11:06
  • Thanks for the clarification @philnash. Answer has been updated. – NM Pennypacker Sep 28 '17 at 12:55