我有一个Ruby程序读取文件并返回特定的输出。我必须现在使用Sinatra创建此程序的Web应用程序。我创建了一个包含所有文件选项的form
,并且现在我想要在按下提交按钮后从表单中选择该文件来运行该Ruby代码。从HTML表单运行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>
我在home.erb中有<%= rsg(params [:grammar_file])%>,但是它打印到控制台而不是页面 – msc
将它放在post方法中,如rsg(params [:grammar_file])也打印在控制台...不知道为什么 – msc
什么?我被卡住了几小时 – msc