Call controller method with rails-Ajax?

16,023

I am not sure I see your goal completely, however, if you just want to call a controller action when clicking on your button, I would approach it like this:

Here the HTML:

<a href='#' id='executer-button' class='btn btn-default'>Executer</a>

You should place this in you app/assets/javascripts/execute_caller.js:

//This function calls the "execute" controller action, using its route and also
//passes a "selectingCommand" variable to it
var callExecuter=function(){
  $.ajax({
    type:'GET',
    url:'/executer_route',
    data: { selectingCommand : document.getElementById("CommandSelect"); 
          },
    success:function(){
      //I assume you want to do something on controller action execution success?
      $(this).addClass('done');
    }
  });
}

$(document).on("click","#executer-button",callExecuter);

Then:

  1. Check your rake routes to see which is the appropiate route to the controller action execute.
  2. Replace it in the url field of the $.ajax(...) function.
  3. Assuming you have either debuggeror pry gem, write debugger inside your def execute action controller, to make sure you reach there via ajax.
  4. Edit your on success action in the ajax call accordingly, so it does what you want it to do.
Share:
16,023
Guillaume Caillé
Author by

Guillaume Caillé

Updated on June 27, 2022

Comments

  • Guillaume Caillé
    Guillaume Caillé over 1 year

    I am trying to execute a Ruby method in my application_controller.rb from a button in my view. In a post yesterday, someone told me to use an Ajax call to do so because, without it, would just run on page load.

    I'm very new to this and I have a hard time understanding it.

    I installed the rails-Ajax gem, and it is added to my Gem file.

    I followed "Integrate Ajax capabilities to Rails websites with history, bookmarking, partial refreshes, Rails flashes, user callbacks, scripts execution, redirections." untill step 5.

    I just don't know what to do it next.

    Here is my current configuration. The method only runs on page load:

    My view:

    <td><button type="button" onclick="executeit()" class="btn btn- default">executer</button></td>
    
    <script>
    function executeit() {
     var selectingCommand = document.getElementById("CommandSelect");
     var selectedCommand = selectingCommand.options[selectingCommand.selectedIndex].text;
     var selectingServer = document.getElementById("serverlist");
     var selectedServer = selectingServer.options[selectingServer.selectedIndex].text;
     var username=document.getElementById("login").text;
     var password=document.getElementById("password").text;
    
    
    
    <%execute%>;
    
    }
    </script>
    

    My method in application_controller.rb :

    def execute
      require 'rubygems'
      require 'net/ssh'
    
      @hostname = "smtlmon02"
      @username = "test"
      @password = "1234"
      @cmd = "ls -al"
      @cmd2 = "sudo su - -c 'ls;date'"
      @cmd3 = "ls -alrt"
    
      ssh = Net::SSH.start(@hostname, @username, :password => @password)
      res = ssh.exec!(@cmd)
      res2 = ssh.exec!(@cmd2)
    
      ssh.close
      puts res2
    
    end
    

    It would be fantastic if anyone could explain how to do it!

  • Guillaume Caillé
    Guillaume Caillé over 9 years
    Can I kindly ask in which file I have to use this code ? :)
  • Guillaume Caillé
    Guillaume Caillé over 9 years
    I will take a guess here, correct me if I am wrong: I need to replace "button" by my button's id. I need to replace function() by my method's name: "execute" And I need to replace test.html by my view's name : "test.html.erb" ?
  • Guillaume Caillé
    Guillaume Caillé over 9 years
    Hi, I just can't seem to understamd how to make it work.. Could you please explain a little bit more ?
  • Saggex
    Saggex over 9 years
    What does the data:-line do? Is it necessary? I'm working on a pretty similar problem.
  • Saggex
    Saggex over 9 years
    Can I somehow pass information to it? Like this.getAttribute('w_id');
  • lllllll
    lllllll over 9 years
    that is exactly what data: does, it's used to attach data to the request, in the example I'm sending document.getElementById("CommandSelect") as selectingCommand variable, which will be present in params[:selectionCommand] inside the controller. Also I recommend you to start a new question even if the issue is similar ;)
  • Admin
    Admin over 8 years
    As much as I appreciate the answer, 'execute' as sample controller name is melting mind of a reader here... Why not something actually meaningful?
  • Giovanni Di Toro
    Giovanni Di Toro over 4 years
    This is not a complete answer. Please, explain further so that all can understand how you got to this code.