title: How to execute ruby function with attributes using AJAX request in Rails 6.1?
body: <p>I have the following home controller:</p>
  <pre><code>class HomeController &lt; ApplicationController
    def index
      @data = EmergencyFriend.all
      @jsonData = JSON.pretty_generate(@data.as_json)
    end
  
    def about
    end
  
    def alertEmergencyContant
      account_sid = &quot;my id&quot;
      auth_token = &quot;my token&quot;
  
      @client = Twilio::REST::Client.new(account_sid, auth_token)
  
      @client.messages.create(
          to: &quot;+number 1&quot;,
          from: &quot;+number 2&quot;,
          body: &quot;hello world !&quot;
      )
    end
  end
  </code></pre>
  <p>Basically, in my <code>home/index.html.erb</code> there is only one button. When the button is pressed it shows an alert message that allows user to select an option to send an SMS to.</p>
  <p><a href="https://i.stack.imgur.com/YkpSs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YkpSs.png" alt="IMAGE DEMO" /></a></p>
  <p>What I want to do is to call the <code>alertEmergencyContant</code> method in my home controller so that I can send the message. I also want to pass the phone_number as a parameter with that request. It has been suggested that for this I should use AJAX. I successfully installed jquery and ajax in my rails project and works as expected. What I can't understand is how to create it as a POST request.</p>
  <p>My routes list for the home directory are :</p>
  <pre><code>root       GET       /                          home#index
  root       GET       /home/about(.:format)      home#about
  </code></pre>
  <p>But there is nothing on <code>alertEmergencyContant</code>. How to declare that in the routes and make it as a POST request? How to pass attributes from JavaScript to ruby using AJAX?</p>
  <p>Here is my ajax request so far (This works):</p>
  <pre><code>$.ajax({
      url: '/',
      type: 'GET',
      success: function(event){
          alert(&quot;sending Message&quot;);
      }
  });
  </code></pre>
  <hr />
  <p><strong>UPDATE:</strong></p>
  <pre><code>def about   
      @thisNumber = params[:phone_number] 
      puts &quot;helllloooooooooooooo        &quot; + @thisNumber
  end
  </code></pre>
  <pre><code>function ajaxRequest(){
      $.ajax({
          url: 'home/about/?phone_number:1244211',
          type: 'GET',
          success: function(event){
              alert(&quot;passed&quot;);
          },
          failed: function(){
              alert(&quot;has failed&quot;)
          },
          done: function(){
              alert(&quot;after&quot;)
          }
      });
  }
  </code></pre>