【问题标题】:Call controller method with rails-Ajax?用rails-Ajax调用控制器方法?
【发布时间】:2014-03-19 18:27:04
【问题描述】:

我正在尝试通过视图中的按钮在 application_controller.rb 中执行 Ruby 方法。 在昨天的一篇文章中,有人告诉我使用 Ajax 调用来执行此操作,因为没有它,只会在页面加载时运行。

我对此很陌生,我很难理解它。

我安装了 rails-Ajax gem,并将它添加到我的 Gem 文件中。

我一直关注“Integrate Ajax capabilities to Rails websites with history, bookmarking, partial refreshes, Rails flashes, user callbacks, scripts execution, redirections.”直到第 5 步。

我只是不知道下一步该做什么。

这是我当前的配置。该方法仅在页面加载时运行:

我的看法:

<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

如果有人能解释怎么做就太好了!

【问题讨论】:

    标签: jquery ruby-on-rails ruby ajax


    【解决方案1】:

    我不确定我是否完全看到了您的目标,但是,如果您只想在单击按钮时调用控制器操作,我会这样处理:

    这里是 HTML:

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

    你应该把这个放在你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);
    

    然后:

    1. 检查您的rake routes,看看哪个是控制器操作execute 的合适路径。
    2. 将其替换为$.ajax(...) 函数的url 字段。
    3. 假设您有 debuggerpry gem,请在您的 def execute 操作控制器中写入 debugger,以确保您通过 ajax 到达那里。
    4. 在 ajax 调用中相应地编辑您的 on success 操作,以便它执行您希望它执行的操作。

    【讨论】:

    • data:-line 有什么作用?有必要吗?我正在解决一个非常相似的问题。
    • 我能以某种方式将信息传递给它吗?喜欢this.getAttribute('w_id');
    • 这正是data: 所做的,它用于将数据附加到请求中,在示例中我将document.getElementById("CommandSelect") 作为selectingCommand 变量发送,它将出现在params[:selectionCommand] 中控制器内部。此外,即使问题相似,我也建议您开始一个新问题;)
    • 尽管我很欣赏这个答案,但作为示例控制器名称的“执行”正在融化读者的心……为什么没有真正有意义的东西?
    【解决方案2】:

    如果你使用 jQuery,你可以添加这个:

    $("button").on("click", function(){
        $.ajax({
            url: "test.html",
            context: document.body
        }).done(
            function() {
                $( this ).addClass( "done" );
            }
        );
    });
    

    【讨论】:

    • 请问我必须在哪个文件中使用此代码? :)
    • 我会在这里猜测一下,如果我错了,请纠正我:我需要用我的按钮 ID 替换“按钮”。我需要用我的方法名称替换 function():“execute” 我需要用我的视图名称替换 test.html:“test.html.erb”?
    • 嗨,我似乎无法理解如何使它工作..你能解释一下吗?
    • 这不是一个完整的答案。请进一步解释,以便所有人都能理解您是如何获得此代码的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2017-05-08
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 2014-07-27
    相关资源
    最近更新 更多