我想從我的視圖中的按鈕在我的application_controller.rb中執行Ruby方法。 在昨天的一篇文章中,有人告訴我使用Ajax調用來這樣做,因爲沒有它,就會在頁面加載時運行。使用rails-Ajax調用控制器方法?
我對此很新,而且我很難理解它。
我安裝了rails-Ajax gem,它被添加到我的Gem文件中。
我只是不知道該怎麼做下一步。
這是我目前的配置。該方法只能運行在頁面加載:
我的觀點:
<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>
我的方法在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
這將是美妙的,如果任何人都可以解釋如何做到這一點!
謝謝我做到了! –
'data:'-line是做什麼的?有必要嗎?我正在研究一個非常類似的問題。 – user3383458
我可以以某種方式將信息傳遞給它嗎?像'this.getAttribute('w_id');' – user3383458