stevie pinero stevie pinero - 7 months ago 14
Ruby Question

How would you assign variables to an api call in a rails controller?

I'm trying to get input from the user and assign that input to a variable and then post to an API.

@options = {
:credentials => {
:account_id => "12345",
:api_key => "mysecretapikey123456"
},
:product => "criminal_database",
:data => {
:FirstName => params[:firstName],
:LastName => params[:lastName],
:MiddleName => params[:middleName],
:Address => params[:address],
:City => params[:city],
:State => params[:state],
:Zip => params[:zip],
:County => params[:county],
:DOB => params[:DOB],
:AgeMin => 0,
:AgeMax => 120,
:Limit => 1,
:ExactMatch => "yes"
}
}
@opt = JSON.generate(@options)
@url = 'https://api.imsasllc.com/v3/'
@response = HTTParty.post(@url, body: @opt, :headers => {'Content-Type' => 'application/json' })
@parsed = JSON.parse(@response.body)
#byebug
p @parsed


Above is how I'm attempting to send everything.

Below is the form that I'm using.

<%= form_for(@record_check) do |f| %>
<% if @record_check.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@record_check.errors.count, "error") %> prohibited this record_check from being saved:</h2>

<ul>
<% @record_check.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>


<%= f.text_field :firstName, :placeholder => "First Name" %>
<%= f.text_field :lastName, :placeholder => "Last Name" %>
<%= f.text_field :middleName, :placeholder => "Middle Name (optional)" %>
<%= f.text_field :address, :placeholder => "Address (optional)" %>
<%= f.text_field :city, :placeholder => "City (optional)" %>
<%= f.text_field :state, :placeholder => "State (optional)" %>
<%= f.text_field :zip, :placeholder => "Zip Code (optional)" %>
<%= f.text_field :county, :placeholder => "County (optional)" %>
<%= f.date_select :DOB, {order: [:month, :day, :year], prompt: { day: 'Select day', month: 'Select month', year: 'Select year' }, start_year: Date.today.year - 18, end_year: Date.today.year - 100}, {required: true} %>
<div class="actions">
<%= f.submit "Get Background Check", class: "btn-submit" %>
</div>
<% end %>


Whenever I submit the form, the API returns an error stating that nothing was posted to it. I'm stumped; I've been working on this for a couple of days... How am I supposed to submit the form input to the method in my controller?

Answer

Looks like you are missing the primary object in your params:

Your @options variable should use params[:record_check][:attribute] instead of params[:attribute].

So params[:firstName] => params[:record_check][:firstName]

Here is my controller record_checks_controller.rb:

class RecordChecksController < ApplicationController

  def new
    @record_check = RecordCheck.new
  end

  def create
    @options = {
    :credentials => {
    :account_id => "XXXXXX",
    :api_key => "MCYTAzCBbulF6X2M2H9tP5lOlsWXX"
    },
    :product => "criminal_database",
      :data => {
      :FirstName => params[:record_check][:first_name],
      :LastName => params[:record_check][:last_name],
      :MiddleName => params[:record_check][:middle_name],
      :Address => params[:record_check][:address],
      :City => params[:record_check][:city],
      :State => params[:record_check][:state],
      :Zip => params[:record_check][:zip],
      :County => params[:record_check][:county],
      :DOB => params[:record_check][:dob],
      :AgeMin => 0,
      :AgeMax => 120,
      :Limit => 1,
      :ExactMatch => "yes"
      }
  }
  @opt = JSON.generate(@options)
  @url = 'https://api.imsasllc.com/v3/'
  @response = HTTParty.post(@url, body: @opt, :headers => {'Content-Type' => 'application/json' })
  @parsed = JSON.parse(@response.body)
  #byebug
  p @parsed
  end
end

My model record_check.rb

class RecordCheck < ActiveRecord::Base
end

And my form new.html.erb:

<%= form_for(@record_check) do |f| %>
  <%= f.text_field :first_name, :placeholder => "First Name" %>
  <%= f.text_field :last_name, :placeholder => "Last Name" %>
  <%= f.text_field :middle_name, :placeholder => "Middle Name (optional)" %>
  <%= f.text_field :address, :placeholder => "Address (optional)" %>
  <%= f.text_field :city, :placeholder => "City (optional)" %>
  <%= f.text_field :state, :placeholder => "State (optional)" %>
  <%= f.text_field :zip, :placeholder => "Zip Code (optional)" %>
  <%= f.text_field :county, :placeholder => "County (optional)" %>
  <%= f.date_select :dob, {order: [:month, :day, :year], prompt: { day: 'Select day', month: 'Select month', year: 'Select year' }, start_year: Date.today.year - 18, end_year: Date.today.year - 100}, {required: true} %>
  <div class="actions">
    <%= f.submit "Get Background Check", class: "btn-submit" %>
  </div>
<% end %>

My create.html.erb view:

<%= @parsed %>

My schema for RecordCheck:

  create_table "record_checks", force: :cascade do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.string   "middle_name"
    t.string   "address"
    t.string   "city"
    t.string   "state"
    t.string   "zip"
    t.string   "county"
    t.date     "dob"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

Resulting view:

{"Results"=>{"Time"=>0.20036697387695, "Message"=>"NO RECORDS FOUND", "TotalRecords"=>0, "Inputs"=>{"Account"=>"128006", "FirstName"=>"John", "LastName"=>"Doe", "AgeMin"=>1, "AgeMax"=>120, "ExactMatch"=>"yes", "Limit"=>1}, "Product"=>"criminal_database", "Records"=>[]}}

That's a pretty good API. I don't have any criminal records, thank god.

Comments