ruby新手,在尝试从视图(index.html.erb)内执行SH脚本时,我遇到了以下问题。我做了以下的事情:
app views 测试index.html.erb
<%= link_to 'Run it', :action=>'callthis' %>
app 控制器 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脚本本质上是不安全的,除非您非常偏执和小心。其他维护应用程序的人也一样。
尝试为初学者添加以下内容:
配置/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