【发布时间】:2018-04-24 17:36:52
【问题描述】:
我有一个读取文件并返回特定输出的 Ruby 程序。我现在必须使用 Sinatra 创建该程序的 Web 应用程序。我创建了一个带有所有文件选项的form,现在我想在按下提交按钮后使用表单中选定的文件运行该 Ruby 代码。
基本上,我不知道如何让这个外部 Ruby 程序以用户从 HTML form 中选择的文件名运行。
Ruby 程序 (example.rb) 以定义 def read_grammar_defs(filename) 开头。
// sinatra_main.rb
require 'sinatra'
require 'sinatra/reloader' if development? #gem install sinatra-contrib
require './rsg.rb'
get '/' do
erb :home
end
post '/p' do
//call program to read file with the parameter from form
end
// layout.erb
<!doctype html>
<html lang="en">
<head>
<title><%= @title || "RSG" %></title>
<meta charset="UTF8">
</head>
<body>
<h1>RubyRSG Demo</h1>
<p>Select grammar file to create randomly generated sentence</p>
<form action="/p" method="post">
<select name="grammar_file">
<option value="Select" hidden>Select</option>
<option value="Poem">Poem</option>
<option value="Insult">Insult</option>
<option value="Extension-request">Extension-request</option>
<option value="Bond-movie">Bond-movie</option>
</select>
<br><br>
</form>
<button type="submit">submit</button>
<section>
<%= yield %>
</section>
</body>
</html>
【问题讨论】: