从html表单提交运行Ruby文件 [英] Run Ruby file from html form submit

查看:135
本文介绍了从html表单提交运行Ruby文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ruby程序读取一个文件并返回一个特定的输出。我必须现在使用Sinatra创建此程序的Web应用程序。我使用所有文件选项创建了一个表单,并且现在我想在按下提交按钮后从表单中选择该文件来运行该代码。



基本上,我不知道如何让这个外部Ruby程序与用户从HTML 窗体 $ b

Ruby程序( example.rb )从定义开始, def read_grammar_defs(filename)

  // sinatra_main.rb 

要求'sinatra'
是否需要'sinatra / reloader'如果开发? #gem install sinatra-contrib
require'./rsg.rb'

get'/'do
erb:home
end

post'/ p'do
//调用程序以形式读取文件
end




// layout.erb

<!doctype html>
< html lang =en>
< head>
< title><%= @title || RSG%>< / title>
< meta charset =UTF8>
< / head>
< body>
< h1> RubyRSG Demo< / h1>
< p>选择语法文件以创建随机生成的句子< / p>
< form action =/ pmethod =post>
< select name =grammar_file>
< option value =选择隐藏>选择< / option>
< option value =诗>诗< / option>
< option value =Insult> Insult< / option>
< option value =扩展请求>扩展请求< / option>
< option value =Bond-movie> Bond-movie< / option>
< / select>
< br>< br>
< / form>
<按钮类型=提交>提交< /按钮>
<节>
<%= yield%>
< / section>
< / body>
< / html>


解决方案

最简单的方法如下:


$ b


  1. example.rb 代码打包到类或模块中,如下所示:

      class FileReader 
    def self.read_grammar_defs(filename)
    #...
    end
    end


  2. 需要来自您的sinatra服务器的文件


  3. 发布操作中,读取参数并调用方法:

    <$
    $ b $结束
    $结束= FileReader.read_grammar_defs(params [:grammar_file])
    erb:home
    结束


使用此代码,提交表单后, code> @result 变量并呈现:home 模板。实例变量可以从ERB访问,所以如果你想显示结果,你可以从那里访问它。



然而,这是一个潜在的问题 - 当页面呈现的URL仍然会说your_host.com/p,如果用户重新加载页面,他们将得到一个404 /route not found错误,因为存在没有得到/ p定义。



作为一种解决方法,您可以重定向'/'并使用会话,如在此StackOverflow答案 Sinatra'官方常见问题解答传递结果值。


I have a Ruby program that reads a file and returns a certain output. I have to now create a web app of this program using Sinatra. I created a form with all the file options and I want to now run that Ruby code with that selected file from the form after the submit button is pressed.

Basically, I’m not sure how to get this external Ruby program to run with the the filename that was selected by the user from the HTML form.

The Ruby program (example.rb) starts with the definition 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>

解决方案

The easiest way is as follows:

  1. Package the example.rb code into a class or module like so:

    class FileReader
      def self.read_grammar_defs(filename)
        # ...
      end
    end
    

  2. require the file from your sinatra server

  3. Inside the post action, read the params and call the method:

    post '/p' do
      @result = FileReader.read_grammar_defs(params[:grammar_file])
      erb :home
    end
    

With this code, after submitting the form, it would populate the @result variable and render the :home template. Instance variables are accessible from ERB and so you could access it from therer if you wanted to display the result.

This is one potential issue with this, though - when the page is rendered the url will still say "your_host.com/p" and if the user reloads the page, they will get a 404 / "route not found" error because there is no get "/p" defined.

As a workaround, you can redirect '/' and use session as described in this StackOverflow answer or Sinatra' official FAQ to pass the result value.

这篇关于从html表单提交运行Ruby文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆