title: Send JSON Response Data to a Custom Widget - Ruby Rails 5.2
body: <p>I'm building a widget that a client can display on their third party website. The widget accepts input from consumers about what kind of house they are looking for. On submit, the widget makes a POST request on a search model within the endpoint API (my database). Then I want to display the results of the search on the client's website.</p>
  <p>So far I've successfully posted the search form on a third party website, and on submit a new search object is created and the search results are ready for view on my website. But I want the search results to be sent back to the client's website.</p>
  <p>On the client's website, they have a js script that looks like this:</p>
  <pre><code>&lt;script type=&quot;text/javascript&quot; src=&quot;http://localhost:3000/searchapis/search_form.js&quot;&gt;&lt;/script&gt;
  </code></pre>
  <p>My routes.rb:</p>
  <pre><code>get 'searchapis/show'
  get '/searchapis/:template', to: &quot;searchapis#show&quot;
  post 'search/create', to: &quot;searches#create&quot;
  </code></pre>
  <p>I created a searchesapi controller that looks like this:</p>
  <pre><code>class SearchapisController &lt; ApplicationController
    protect_from_forgery :except =&gt; :show
    
    def show
      @search = Search.new
      
      respond_to do |format|
        format.html { render params[:template], layout: 'searchapis' }
        format.js   { render js: js_constructor }
      end
    end
    
    private
      def js_constructor
        content = render_to_string(params[:template], layout: false)
        &quot;document.write(#{content.to_json})&quot;
      end
  end
  </code></pre>
  <p>I created a <code>search_form.html.erb</code> view that looks like this:</p>
  <pre><code>&lt;%= bootstrap_form_for @search, :html =&gt; {}, :url =&gt; create_search_url(@search), remote: true do |form| %&gt;
  
  &lt;%=form.select :beds, (select_options), {prompt: &quot;Min # of Bedrooms&quot;, hide_label: true}, {class: &quot;form-control form-control-sm&quot;}%&gt;
  
  &lt;%=form.select :max_price, (100000..2000000).step(10000).map{|x| number_to_currency(x, precision: 0)}, {hide_label: true, prompt: &quot;Select Max Price&quot;}, {class: &quot;form-control form-control-sm&quot;, id: &quot;sale_price&quot;}%&gt;
  
  &lt;input type=&quot;submit&quot; name=&quot;commit&quot; value=&quot;SEARCH&quot; class=&quot;btn btn-danger&quot; data-disable-with=&quot;Finding Schools...&quot;&gt;
  </code></pre>
  <p>And finally the searches_controller.rb:</p>
  <pre><code>class SearchesController &lt; ApplicationController
    skip_before_action :verify_authenticity_token
    before_action :set_search, only: [:show, :edit, :update, :destroy]
    before_action :require_admin, only: [:index, :destroy]
  
    def create
        @search = Search.new(search_params)
  
        respond_to do |format|
          if @search.save
  
            format.html { redirect_to @search, notice: 'Search was successfully created.'}
            results = @search.listings
            p results #this is what I want to send to the third party site
            format.json { render json: results.to_json, remote: true, notice: &quot;Search results below&quot;}
          end
        end
    end
  end
  </code></pre>
  <p>Now the form is displayed, and on submit, a new search is created, but I'm not sure how to proceed so the search results can be sent via a JSON response to the third party website.</p>
  <p>I'm thinking I'll need to produce an additional javascript src for the client's website =(<code>&lt;script type=&quot;text/javascript&quot; src=&quot;http://localhost:3000/searchapis/api_search_results.js&quot;&gt;&lt;/script&gt;</code>) linked to a new api_search_results page, and I'll need to <code>rails g migration AddNewSttributeToSearch api:boolean</code> so I can catch an api search in the controller and route to the new api_search_results page. Then, when a consumer hits submit on the third party website, the 2nd javascript src would display the results, but I think perhaps there is a better way. Any thoughts out there?</p>