我想从我的视图中的按钮在我的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