如何从rails视图执行ruby脚本

时间:2022-05-17 00:01:10

I am new to ruby on rails and trying to build an internal site to manage several ruby scripts. What I am trying to accomplish; create web page where an user clicks on a link (Execute Analyzer) to execute ruby script (runs in the background - FitAnalyzer.rb). I am having trouble understanding how to route this without redirecting to another web page. I basically need help how to add the correct format to routes.rb.

我是ruby on rails的新手,并尝试构建一个内部站点来管理几个ruby脚本。我想要完成的事情;创建用户点击链接(Execute Analyzer)以执行ruby脚本(在后台运行 - FitAnalyzer.rb)的网页。我无法理解如何在不重定向到其他网页的情况下进行路由。我基本上需要帮助如何将正确的格式添加到routes.rb。

analyzer.html.erb

<%= link_to "![Execute][1] Analyzer", :controller =>
'fit_anlayzer_controller', :method => 'execute_analyzer'%>

fit_analyzer_controller.rb

class FitAnalyzerController < ApplicationController
  def analyzer
  end

  def execute_analyzer
    # Delayed::Job.enqueue(AnalyzerJob.new(params[:id]), 3)
    # command = 'ruby ~/ruby_scripts/fit_analyzer/FitAnalyzer.rb'
    @execute_command = system 'ruby
/Users/gsypolt/rubyqa_project/ruby_scripts/fit_analyzer/FitAnalyzer.rb'
    flash[:notice] = "Executing FitNesse Analyzer"
  end
end

When accessing mylocalhost/fit_analyzer/analyzer receiving this message below

访问下面收到此消息的mylocalhost / fit_analyzer / analyzer时

No route matches {:controller=>"fit_anlayzer_controller",
:method=>"execute_analyzer", :popup=>true}

I have been learning ruby on rails from lyndia.com and book (rails 3 way)

我一直在从lyndia.com学习ruby并预订(rails 3路)

mockup screenshot what i am trying to build

mockup截图我想要构建的内容

1 个解决方案

#1


-1  

You need to add a route

您需要添加路线

# config/routes.rb
match "fit_analyzer/analyzer" => 'fit_analyzer#execute_analyzer'

The fit_analyzer#execute_analyzer corresponds to controller#action

fit_analyzer#execute_analyzer对应于controller#action

Then you need to call it using ajax. To do that add a :remote => true to your link_to

然后你需要使用ajax调用它。为此,在link_to中添加:remote => true

#1


-1  

You need to add a route

您需要添加路线

# config/routes.rb
match "fit_analyzer/analyzer" => 'fit_analyzer#execute_analyzer'

The fit_analyzer#execute_analyzer corresponds to controller#action

fit_analyzer#execute_analyzer对应于controller#action

Then you need to call it using ajax. To do that add a :remote => true to your link_to

然后你需要使用ajax调用它。为此,在link_to中添加:remote => true