用rails-Ajax调用控制器方法? [英] Call controller method with rails-Ajax?
问题描述
我正在尝试通过视图中的按钮在application_controller.rb中执行Ruby方法. 在昨天的一篇文章中,有人告诉我使用Ajax调用来这样做,因为如果没有它,它将仅在页面加载时运行.
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.
我安装了rails-Ajax gem,并将其添加到我的Gem文件中.
I installed the rails-Ajax gem, and it is added to my Gem file.
我遵循了"将Ajax功能与历史记录,书签添加和部分刷新功能集成到Rails网站,Rails闪烁,用户回调,脚本执行,重定向.,直到第5步.
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:
我的观点:
<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中的方法:
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!
推荐答案
我不确定我是否完全看到了您的目标,但是,如果您只是想在单击按钮时调用控制器动作,我会采用这种方式:
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:
以下是HTML:
<a href='#' id='executer-button' class='btn btn-default'>Executer</a>
您应该将其放置在您的app/assets/javascripts/execute_caller.js
中:
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);
然后:
- 检查您的
rake routes
,以了解哪个是通往控制器操作execute
的适当路径. - 将其替换在
$.ajax(...)
函数的url
字段中. - 假设您有
debugger
或pry
宝石,请在def execute
动作控制器内写入debugger
,以确保您通过ajax到达那里. - 相应地在ajax调用中编辑
success
上的操作,以便它执行您想要的操作.
- Check your
rake routes
to see which is the appropiate route to the controller actionexecute
. - Replace it in the
url
field of the$.ajax(...)
function. - Assuming you have either
debugger
orpry
gem, writedebugger
inside yourdef execute
action controller, to make sure you reach there via ajax. - Edit your on
success
action in the ajax call accordingly, so it does what you want it to do.
这篇关于用rails-Ajax调用控制器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!