Search code examples
ruby-on-rails-5aiml

GO button does nothing (AIML rails 5)


Console tab

Network tab

I have installed programr. I followed tutorial from this site http://dreamingechoes.github.io/bot/ruby/rails/conversational-bot-ruby-on-rails/

bot.rb

require 'programr'

brains = Dir.glob("lib/bot/*")

BOT = ProgramR::Facade.new
BOT.learn(brains)

application_controller.rb

def ask_bot
  reaction = BOT.get_reaction(params[:query])
  render json: { response: reaction.present? ? reaction : "I don't have an answer to that"  }
end

bot.aiml

<?xml version="1.0" encoding="UTF-8"?>

 <aiml version="1.0" xmlns="http://alicebot.org/2001/AIML-1.0.1"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://alicebot.org/2001/AIML-1.0.1 http://aitools.org/aiml/schema/AIML.xsd">

<category>
  <pattern>Hello *</pattern>
  <template>
    Hey, how can I help you?
  </template>
</category>

<category>
  <pattern>*bye</pattern>
  <template>
    Always here for you!
  </template>
</category>

<category>
  <pattern>What payment methods do you accept?</pattern>
  <template>
    We accept Visa, MasterCard and American Express.
  </template>
</category>

</aiml>

view:

<div class="alert alert-info">
  <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <strong>Info!</strong> Type something on the text input and hit the <strong>GO</strong> button to get a response.
</div>

<div class="bs-callout bs-callout-info bot-response hide" id="callout-alerts-no-default">
 <h4>Bot says:</h4>
 <p id="bot-response"> </p>
</div>

<div class="row home-row">
  <div class="col-lg-12">
    <div class="input-group">
      <input id="query" type="text" class="form-control" placeholder="Say something to the bot...">
     <span class="input-group-btn">
      <button id="ask" class="btn btn-default" type="button">GO</button>
     </span>
   </div>
 </div>
</div>

application.js

$(document).ready(function(){

  $('#ask').on('click', function(event) {
    $.ajax({
    url: '/ask_bot',
    type: 'json',
    method: 'get',
    data: { query: $('#query').val() },
    success: function(data) {
    $('.bot-response').removeClass('hide');
    $('#bot-response').html(data['response']);
    $('#query').val('');
   }
  });
 });

});

If i copy a pattern and paste in the form, the GO button does nothing. Please help...thanks!


Solution

  • Most probably what you'll have to use is the DOMContentLoaded in order to check when the DOM has finished loading, this way you can wrap the listener for the #ask button and the AJAX function, without having to modify your "requires" in the application.js file, nor to have to include them as script tags in your views.

    You could try with something like:

    document.addEventListener('DOMContentLoaded', function () {
      ...
    });
    

    And inside to put your JS code, which would remain as:

    document.addEventListener('DOMContentLoaded', function () {
      $('#ask').on('click', function(event) {
        $.ajax({
          url: '/ask_bot',
          type: 'json',
          method: 'get',
          data: { query: $('#query').val() },
          success: function(data) {
            $('.bot-response').removeClass('hide');
            $('#bot-response').html(data['response']);
            $('#query').val('');
          }
        });
      });
    
      $('[data-toggle="tooltip"]').tooltip();
    });
    

    Also you could add the specific custom.js for the specific controller and action in which is being used, that's the reason of the Uncaught TypeError: Cannot read property 'getContext' of null error, because such elements barChart and barChart2 are in the back_office#index.