简体   繁体   English

我如何将一个小的 ruby 应用程序“插入”到 Rails 上的 Ruby 以制作 web 页面?

[英]How can i 'insert' a small ruby app into Ruby on Rails to make a web page?

I have a small ruby application that I made on my local machine.我有一个在本地机器上创建的小型 ruby 应用程序。 It uses a text file insted of a database.它使用插入数据库的文本文件。 It's a simple app that takes in a word, processes it against the text file, and then outputs the results using puts .这是一个简单的应用程序,它接收一个单词,根据文本文件对其进行处理,然后使用puts输出结果。

I would like to fit it into a RoR framework, hosted on my personal machine.我想将它放入我个人机器上托管的 RoR 框架中。 I have run through some lessons and tutorials in a few books and online materials I have, but they all involve databases.我在一些书籍和在线资料中浏览了一些课程和教程,但它们都涉及数据库。

I read thru the notes in config/environment.rb and found at line 19 instructions to uncomment the line that removes ActiveRecord.我通读了config/environment.rb中的注释,并在第 19 行找到了取消注释删除 ActiveRecord 的行的说明。

I am currently looking for the appropriate directories to put the text file itself, and the code from the ruby app that reads this text file.我目前正在寻找合适的目录来放置文本文件本身,以及来自读取此文本文件的 ruby 应用程序的代码。 Thank you.谢谢你。

This sounds like a better fit for something more lightweight like Sinatra .这听起来更适合像Sinatra这样更轻量级的东西。

If you were bound and determined to use Rails, you could make a controller with an action that just runs the code from your program, but it seems like overkill.如果您已被约束并决心使用 Rails,您可以制作一个 controller,其操作只是从您的程序中运行代码,但这似乎有点矫枉过正。 You wouldn't be using 99.9% of the framework's capabilities, so why is it even there?你不会使用框架 99.9% 的功能,那为什么还存在呢?

You probably want to do something like the following:您可能想要执行以下操作:

  • Have a controller that takes the word as a parameter.有一个以单词为参数的 controller。
  • Turn you app into a function that takes the word as a parameter and returns the results (instead of doing puts)将您的应用程序变成一个 function,它将单词作为参数并返回结果(而不是执行 puts)
  • Call the function from your controller and render the results (eg something like:从 controller 调用 function 并渲染结果(例如:

    render:text => my_func(word)渲染:文本 => my_func(word)

If your just trying to give it a little interface you could look at Shoes which is an easy to use multi-platform gui framework.如果你只是想给它一个小界面,你可以看看 Shoes,它是一个易于使用的多平台 gui 框架。

Or like someone mentioned take a look at Sinatra.或者就像有人提到的那样,看看 Sinatra。

You can simply use rails without worrying about ActiveRecord.您可以简单地使用导轨,而不必担心 ActiveRecord。 I'd sugest making your little application into a class and requiring the file in a controller you want to use (or in environment.rb).我建议将您的小应用程序放入 class 并要求您要使用的 controller 中的文件(或 environment.rb 中)。 Put the file in lib and if the data is temporary, "tmp" is fine or just put it in "lib" with the script you wrote even "db" is a fine location for it.将文件放在 lib 中,如果数据是临时的,“tmp”就可以了,或者只是将它与您编写的脚本一起放在“lib”中,甚至“db”也是一个很好的位置。 To make a view just run your code and put the return in a class variable and make a view for it.要创建视图,只需运行您的代码并将返回值放入 class 变量并为其创建视图。

A model need not inherit from ActiveRecord::Base. model 不需要从 ActiveRecord::Base 继承。 Or anything else.或者别的什么。 As long as it follows the naming convention for models, Rails will pick it up without problem.只要它遵循模型的命名约定,Rails 就会毫无问题地使用它。

Having said that, if you're really looking at a one-model, one-controller, one-action, no-database app, then Sinatra would probably be a really good lightweight place to start...话虽如此,如果您真的在寻找一个模型、一个控制器、一个动作、无数据库的应用程序,那么 Sinatra 可能是一个非常好的轻量级起点......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM