Ruby-on-rails Sinatra 与 Rails

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3989438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:26:35  来源:igfitidea点击:

Sinatra vs. Rails

ruby-on-railsrubysinatra

提问by LoveMeSomeCode

I've worked through some of the Sinatra and Rails samples, but I'm having a hard time figuring out which features belong to which technology.

我已经研究了一些 Sinatra 和 Rails 示例,但我很难弄清楚哪些特性属于哪种技术。

What specifically do I gain by using Sinatra/Rails? Is it just ActionPack/ActionView? Correct me if I'm wrong, but I COULD just use Webrick/Mongrel and serve up my .erb files right? And I could use ActiveRecord technology in those files and still access post variables, session state and querystring variables right?

使用 Sinatra/Rails 有什么特别的好处?它只是 ActionPack/ActionView 吗?如果我错了,请纠正我,但我可以只使用 Webrick/Mongrel 并提供我的 .erb 文件,对吗?而且我可以在这些文件中使用 ActiveRecord 技术,并且仍然可以访问发布变量、会话状态和查询字符串变量,对吗?

So, what I'm asking you guys is, if I start with the PHP-like scenario above; Webrick + ERB + ActiveRecord, what do I gain by using Sinatra? And what do I further gain by using Rails?

所以,我要问你们的是,如果我从上面的类似 PHP 的场景开始;Webrick + ERB + ActiveRecord,使用 Sinatra 有什么好处?使用 Rails 还能进一步获得什么?

采纳答案by ehsanul

For Sinatra, it's really almost like a wrapper around Rack. So you first need to ask what the point of Rack is. Rack is basically a specification for how a framework should return a result, it can use what's returned with any web server that Rack supports. So it's really a compatibility layer that allows you to choose your framework/server combination at will, without worrying about whether they'll work together. If your framework is Rack-compliant, you should be able to deploy on practically any server via Rack.

对于 Sinatra 来说,它几乎就像是 Rack 的包装器。所以你首先需要问Rack的意义是什么。Rack 基本上是一个框架应该如何返回结果的规范,它可以使用 Rack 支持的任何 Web 服务器返回的结果。所以它真的是一个兼容层,让你可以随意选择你的框架/服务器组合,而不必担心它们是否会一起工作。如果您的框架是 Rack 兼容的,您应该能够通过 Rack 部署在几乎任何服务器上。

Now, the thing is Rack is very low level. Frameworks such as Sinatra give you things like nice routing, helpers, before/after filters, and a lot more. You just need to look to the docs to see what you can get. Rails is much more featureful, and in many ways "magical". That is, you might write a single line in Rails that ends up doing quite a lot, which for some is a good thing, and for some too magical. I personally prefer Sinatra for this reason, at least before getting a decent understanding of Rails internals.

现在,Rack 的水平非常低。诸如 Sinatra 之类的框架为您提供了诸如漂亮的路由、帮助程序、前后过滤器等等。你只需要查看文档,看看你能得到什么。Rails 功能更强大,而且在很多方面都很“神奇”。也就是说,你可能在 Rails 中写了一行,结果却做了很多事情,这对某些人来说是件好事,但对某些人来说太神奇了。出于这个原因,我个人更喜欢 Sinatra,至少在对 Rails 内部结构有很好的了解之前。

回答by shingara

The gain by Rails is ActionView/ActionPack. But you can just replace by Mongrel/Erb. It's something different.

Rails 的收获是 ActionView/ActionPack。但是你可以用 Mongrel/Erb 替换。这是不同的东西。

It's all herlper you have in your view like name_route or error management in your form. It's all resources management and all plugin like InheritedResources. The advantage of Rails.

这就是您在视图中拥有的所有帮助程序,例如 name_route 或表单中的错误管理。它是所有资源管理和所有插件,如 InheritedResources。Rails 的优势。

There are some tool like the Padrino environment to help you to have all of this helper. But It's really speeder after all plugin activate ? I don't think so.

有一些像 Padrino 环境这样的工具可以帮助您拥有所有这些帮助程序。但是在所有插件激活之后它真的更快吗?我不这么认为。

With Rails 3, Rails is a complete Rack application with a lot of RackMiddleware. You can just drop off some middleware to increase your response.

在 Rails 3 中,Rails 是一个完整的 Rack 应用程序,带有很多 RackMiddleware。你可以放下一些中间件来增加你的反应。

回答by KSD Putra

This question is still relevant until today. And with rails' features are increasing over the time, I want to add a new answer.

这个问题直到今天仍然有意义。随着 rails 的功能随着时间的推移而增加,我想添加一个新的答案。

There are so many gems right now, so what you can achieve in Rails, most likely you can achieve it too in Sinatra. If we want to compare between Rails and Sinatra (or any other frameworks), we just need to compare the performance and the ease of use.

现在有很多 gem,所以你可以在 Rails 中实现,很可能你也可以在 Sinatra 中实现。如果我们想在 Rails 和 Sinatra(或任何其他框架)之间进行比较,我们只需要比较性能和易用性。

One of Rails doctrineis convention over configuration. When you create a project in Rails, automatically you get many gems included in your Gemfile. Not only gems, when you look at configdirectory, you'll see many things included. This doctrine is the reason why the magic can happen in the first place. Once you break the convention, you have to modify -or even create- your configuration.

其中一个Rails的学说是约定优于配置。当您在 Rails 中创建项目时,您的Gemfile. 不仅是 gems,当你查看config目录时,你会看到很多东西都包含在内。这个学说是魔法可以首先发生的原因。一旦你打破了约定,你就必须修改——甚至创建——你的配置。

When we want to have more flexibility but still not reinvent the wheel, we can use framework like Sinatra, where only not so many features is enabled when we first create the project. Even so, I've created mini rails from Sinatra: I just adopting rails' way, with libs/gems that I really need. Because I need to develop my own configuration, the development time was longer than using rails.

当我们想要有更多的灵活性但又不想重新发明轮子时,我们可以使用像 Sinatra 这样的框架,其中在我们第一次创建项目时只启用了不多的功能。即便如此,我还是从 Sinatra 创建了 mini rails:我只是采用 rails 的方式,使用我真正需要的 libs/gems。因为需要自己开发配置,开发时间比用rails要长。

If you see this web frameworks benchmark, you'll see that Rails is so slow while other ruby framework, like Sinatra, can be found much higher than Rails.

如果你看到这个web frameworks benchmark,你会发现 Rails 太慢了,而其他 ruby​​ 框架,比如 Sinatra,可以发现比 Rails 高得多。

So, when the best to use Rails?

那么,什么时候最好使用Rails?

  • You need fast development time. Just follow the convention;
  • The future features of your apps are still unknown.
  • 您需要快速的开发时间。只需遵守约定即可;
  • 您的应用程序的未来功能仍然未知。

When the best to use Sinatra?

什么时候最好使用 Sinatra?

  • You don't need fast development time;
  • Sinatra can be fast if you only work in mini project;
  • You know that you won't add many features in the future.
  • 您不需要快速的开发时间;
  • 如果你只在小项目中工作,Sinatra 会很快;
  • 您知道将来不会添加许多功能。

What do you gain from Rails? Development speed.

你从 Rails 中获得了什么?开发速度。

What do you gain from Sinatra? Flexibility.

你从辛纳屈那里得到了什么?灵活性。