[英]Calling SH script from RubyOnRails View - Passing arguments and redirecting to an html file on the filesystem afterwards
红宝石的新手,在尝试从视图(index.html.erb)中执行SH脚本时,我偶然遇到了以下问题。 我已经完成以下工作:
app \\ views \\ tests \\ index.html.erb
<%= link_to 'Run it', :action=>'callthis' %>
app \\ controllers \\ tests_controller.rb
def callthis
puts "I was called!"
# system call here
end
我收到以下错误
ActionController::RoutingError (No route matches {:action=>"callthis", controller=>"tests"}):
app/views/tests/index.html.erb:20:in `_app_views_tests_index_html_erb__911976670__617626598'
app/views/tests/index.html.erb:4:in `each'
app/views/tests/index.html.erb:4:in `_app_views_tests_index_html_erb__911976670__617626598'
app/controllers/tests_controller.rb:7:in `index'
编辑:路线
tests GET /tests(.:format) tests#index
POST /tests(.:format) tests#create
new_test GET /tests/new(.:format) tests#new
edit_test GET /tests/:id/edit(.:format) tests#edit
test GET /tests/:id(.:format) tests#show
PUT /tests/:id(.:format) tests#update
DELETE /tests/:id(.:format) tests#destroy
编辑2:感谢Zippy,我看到输出了hello,但是,它也给我以下错误:
Hello
Started GET "/tests/callthis" for 10.10.9.141 at Wed Mar 13 16:29:22 -0400 2013
Processing by TestsController#callthis as HTML
Completed 500 Internal Server Error in 6ms
ActionView::MissingTemplate (Missing template tests/callthis, application/callthis with {:handlers=>[:erb, :builde
:formats=>[:html], :locale=>[:en]}. Searched in:
* "/mnt/wt/ruby/wtrorp/app/views"
):
编辑3:
从“测试”的角度来看,我只希望执行在“ testscontroller”中定义的“ callthis”。 该def只是执行SH脚本并重定向到它生成的输出文件到文件系统的系统调用。 我正在向它传递两个参数:一个字符串(在测试对象“ test.script”中)和一个id(我想从浏览器会话中提取)。 我意识到问题变得更加棘手,并且将更新标题问题以反映该问题:D
system(./callthis.sh test.script $my_id_var)
编辑4:
我正在寻找从sh脚本生成文件,并重定向到静态文件。 静态页面将捕获一个变量,并知道sh脚本生成了哪个文件,以便它将其显示在该静态页面的框架中。 也许不在这个问题的背景下,但是有人可以指出我正确的方向吗?
预先感谢您的宝贵时间!
要解决Missing Template错误,您需要渲染模板,重定向到有效的url,或者至少调用诸如“ render:nothing”或“ head:ok”之类的东西,以便告诉Rails如何响应用户的浏览器。
在清理任何用户输入(包括您说要退出会话的ID)时要非常小心。 假设用户可能已经想出了如何恶意修改它来做一些不好的事情,并对照白名单(可能只有数字或类似的东西)对其进行检查。 除非您非常偏执和谨慎,否则运行这样的shell脚本绝对是不安全的。 其他所有维护该应用程序的人也是如此。
尝试为初学者添加以下内容:
config / routs.rb
resources :tests do
collection do
get 'callthis'
end
end
或者,如果您没有这样的多条路线,则可以这样做:(同一件事)
resources :tests do
get 'callthis', :on => :collection
end
另外,只是提示:不要将您的动作命名为callthis
。 在这里,没有人知道该动作正在尝试做什么,一段时间后您也不会知道。 希望我能帮上忙。
编辑:发问者更新路线后,当我进一步阅读问题后,我将只建议一件事:
像这样声明您的callthis
控制器:
def callthis
#perform your sh script
redirect to tests_path
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.