当前位置:   article > 正文

什么是Ruby on Rails?

ruby on rails

图片来源: RubyonRails.org

纯粹主义者的Web开发框架

早在1995年,松本行弘(Yukihiro Matsumoto)发行了Ruby 0.95,它是一种纯的,面向对象(OO)的通用编程语言:

作为一名语言狂热者和面向对象的粉丝,有15年的历史了,我真的想要一种真正的面向对象,易于使用的脚本语言。 我寻找了,但是找不到。 所以我决定做到这一点。 — Y. Matsumoto

然后在2004年,David Heinemeier Hansson发布了Ruby on Rails的第一个版本作为Web应用程序框架,他使用该版本启动了团队管理应用程序Basecamp 。 他在2005年开源了Ruby on Rails,Apple在2007年与OS X Leopard一起发布了它。

在本教程中,我将Ruby on Rails简称为Rails。 有些人也使用RoR。

您可能认识的各种流行的Web应用程序都使用Rails,例如GitHub,Shopify,Airbnb,Twitch,SoundCloud,Hulu,Zendesk,Square和Highrise。 但是,由于可伸缩性限制而受到批评,尤其是Twitter,Twitter不得不将其许多服务逐渐转移到其他平台。 也就是说,Twitter具有很高的交易率和可扩展性挑战。

什么是带有Juggler的Ruby on Rails示例Rails应用程序

在本教程中,我将回答问题“什么是Ruby on Rails?” 并向您介绍编程框架的基础知识,以帮助您进行实验。

在开始之前,请记住,我参加了下面的讨论。 如果您有任何问题或建议,请在下面发表评论,或通过Twitter @reifman与我联系

什么是Ruby on Rails?

Ruby on Rails是一种流行的,开源的,面向对象的Web开发框架,许多程序员和应用程序提供商都在使用该框架。 它还基于模型视图控制器(MVC)方法。

模型映射到数据库,功能映射到应用程序中的对象,例如用户。 Rails为命名和文件目录结构提供了一个标准约定,该约定简化了编程,还提供了自动构建功能的解决方案,称为脚手架。 它还确保了开发人员可以共同工作并从彼此的努力中受益的共同环境。

通过约定而不是配置来优化程序员的满意度是我们的工作方式。

Controller主要在用户和服务器之间管理请求,从模型收集数据并通过View文件将其返回给用户。

视图本质上是编程HTML文件,它们用动态数据布置页面。

Rails约定指导开发人员依赖RESTful路由。 控制器动作通常是新动作,创建动作,编辑动作,更新动作,销毁动作,显示动作,索引动作等。这些动作使用户和系统可以与各种功能进行交互

总的来说,Rails的MVC功能称为Action Pack,即ActionController,ActionView和ActiveRecord,后者用于与数据库进行交互。

什么是Ruby on Rails Action Pack View Controller记录

Rails的创始人汉森(Hansson)在名为Ruby on Rails的框架背后提供了原理性神学。

什么是Ruby on Rails Rails原理

Ruby on Rails学说

Rails原则包含八个原则:

  1. 优化程序员的幸福感 :Ruby尽可能满足程序员的需求。
  2. 约定优于配置 :选择标准的,平台范围的默认选项可以使程序员更快速,更轻松地开始新项目。
  3. 菜单是omakase :Ruby on Rails背后的团队已经选择了最好的工具和方法,每个人都将更高兴使用它们。
  4. 没有一个范例 :Rails最终为各种场合提供了灵活的解决方案。
  5. 高尚美丽的代码 :主要关注美观和简单。
  6. 价值集成系统 :一种方向,旨在完全了解和凝聚构建应用程序和解决方案所需的所有工具。
  7. 超越稳定性的进步 :愿意冒险将平台向前推进。
  8. 推开一个大帐篷 :欢迎广大参与者和他们的声音来设定愿景。
Rails的主要成就是围绕围绕编程和程序员本质的一系列异端思想团结并培养一个强大的部落。 —大卫·海尼迈尔·汉森

以下是当前主要的Rails贡献者(创始人David Hansson在左上方):

什么是Ruby on Rails核心团队

他们确实有一个热情的社区:

也许您有兴趣进一步学习,所以这里是开始的方法。

Ruby on Rails入门

Rails收集了许多出色的文档。 这是《 入门指南》的一瞥:

什么是Ruby on Rails入门

该指南将引导您完成一般介绍,基本安装和一些初学者的Rails编程。

在Rails上安装Ruby

Mac用户会发现预装了Ruby。 Windows用户可以签出RailsInstaller 。 要在OS X上完整安装Rails,本指南建议Tokaido 。 另外,Daniel Kehoe建议避免使用一键式安装程序,并提供有关Mac OS X El Capitan安装的详细说明

有经验的Rails开发人员还报告说,它使Web应用程序开发更加有趣。 — Rails指南

但是,自从我运行Rails以来已经有一段时间了,并且在El Capitan上的最新型号Mac上重新安装它非常耗时。

验证Ruby的安装

在OS X上,将预安装Ruby:

  1. $ ruby -v
  2. ruby 2.0.0p645 (2015-04-13 revision 50299) [universal.x86_64-darwin15]

安装Rails框架

我的Rails安装大约花了10到15分钟(请耐心等待):

  1. $ sudo gem install rails
  2. Password:
  3. Fetching: thread_safe-0.3.5.gem (100%)
  4. Successfully installed thread_safe-0.3.5
  5. Fetching: tzinfo-1.2.2.gem (100%)
  6. Successfully installed tzinfo-1.2.2
  7. Fetching: minitest-5.8.4.gem (100%)
  8. Successfully installed minitest-5.8.4
  9. Fetching: activesupport-4.2.5.1.gem (100%)
  10. Successfully installed activesupport-4.2.5.1
  11. Fetching: rack-1.6.4.gem (100%)
  12. Successfully installed rack-1.6.4
  13. Fetching: rack-test-0.6.3.gem (100%)
  14. Successfully installed rack-test-0.6.3
  15. Fetching: mini_portile2-2.0.0.gem (100%)
  16. Successfully installed mini_portile2-2.0.0
  17. Fetching: nokogiri-1.6.7.2.gem (100%)
  18. Building native extensions. This could take a while...
  19. Successfully installed nokogiri-1.6.7.2
  20. Fetching: loofah-2.0.3.gem (100%)
  21. Successfully installed loofah-2.0.3
  22. Fetching: rails-html-sanitizer-1.0.3.gem (100%)
  23. Successfully installed rails-html-sanitizer-1.0.3
  24. Fetching: rails-deprecated_sanitizer-1.0.3.gem (100%)
  25. Successfully installed rails-deprecated_sanitizer-1.0.3
  26. Fetching: rails-dom-testing-1.0.7.gem (100%)
  27. Successfully installed rails-dom-testing-1.0.7
  28. Fetching: builder-3.2.2.gem (100%)
  29. Successfully installed builder-3.2.2
  30. Fetching: erubis-2.7.0.gem (100%)
  31. Successfully installed erubis-2.7.0
  32. Fetching: actionview-4.2.5.1.gem (100%)
  33. Successfully installed actionview-4.2.5.1
  34. Fetching: actionpack-4.2.5.1.gem (100%)
  35. Successfully installed actionpack-4.2.5.1
  36. Fetching: activemodel-4.2.5.1.gem (100%)
  37. Successfully installed activemodel-4.2.5.1
  38. Fetching: arel-6.0.3.gem (100%)
  39. Successfully installed arel-6.0.3
  40. Fetching: activerecord-4.2.5.1.gem (100%)
  41. Successfully installed activerecord-4.2.5.1
  42. Fetching: globalid-0.3.6.gem (100%)
  43. Successfully installed globalid-0.3.6
  44. Fetching: activejob-4.2.5.1.gem (100%)
  45. Successfully installed activejob-4.2.5.1
  46. Fetching: mime-types-2.99.gem (100%)
  47. Successfully installed mime-types-2.99
  48. Fetching: mail-2.6.3.gem (100%)
  49. Successfully installed mail-2.6.3
  50. Fetching: actionmailer-4.2.5.1.gem (100%)
  51. Successfully installed actionmailer-4.2.5.1
  52. Fetching: thor-0.19.1.gem (100%)
  53. Successfully installed thor-0.19.1
  54. Fetching: railties-4.2.5.1.gem (100%)
  55. Successfully installed railties-4.2.5.1
  56. Fetching: bundler-1.11.2.gem (100%)
  57. Successfully installed bundler-1.11.2
  58. Fetching: concurrent-ruby-1.0.0.gem (100%)
  59. Successfully installed concurrent-ruby-1.0.0
  60. Fetching: sprockets-3.5.2.gem (100%)
  61. Successfully installed sprockets-3.5.2
  62. Fetching: sprockets-rails-3.0.1.gem (100%)
  63. Successfully installed sprockets-rails-3.0.1
  64. Fetching: rails-4.2.5.1.gem (100%)
  65. Successfully installed rails-4.2.5.1
  66. Parsing documentation for thread_safe-0.3.5
  67. Installing ri documentation for thread_safe-0.3.5
  68. Parsing documentation for tzinfo-1.2.2
  69. Installing ri documentation for tzinfo-1.2.2
  70. Parsing documentation for minitest-5.8.4
  71. Installing ri documentation for minitest-5.8.4
  72. Parsing documentation for activesupport-4.2.5.1
  73. unable to convert "\x84" from ASCII-8BIT to UTF-8 for lib/active_support/values/unicode_tables.dat, skipping
  74. Installing ri documentation for activesupport-4.2.5.1
  75. Parsing documentation for rack-1.6.4
  76. Installing ri documentation for rack-1.6.4
  77. Parsing documentation for rack-test-0.6.3
  78. Installing ri documentation for rack-test-0.6.3
  79. Parsing documentation for mini_portile2-2.0.0
  80. Installing ri documentation for mini_portile2-2.0.0
  81. Parsing documentation for nokogiri-1.6.7.2
  82. unable to convert "\xCA" from ASCII-8BIT to UTF-8 for lib/nokogiri/nokogiri.bundle, skipping
  83. Installing ri documentation for nokogiri-1.6.7.2
  84. Parsing documentation for loofah-2.0.3
  85. Installing ri documentation for loofah-2.0.3
  86. Parsing documentation for rails-html-sanitizer-1.0.3
  87. Installing ri documentation for rails-html-sanitizer-1.0.3
  88. Parsing documentation for rails-deprecated_sanitizer-1.0.3
  89. Installing ri documentation for rails-deprecated_sanitizer-1.0.3
  90. Parsing documentation for rails-dom-testing-1.0.7
  91. Installing ri documentation for rails-dom-testing-1.0.7
  92. Parsing documentation for builder-3.2.2
  93. Installing ri documentation for builder-3.2.2
  94. Parsing documentation for erubis-2.7.0
  95. Installing ri documentation for erubis-2.7.0
  96. Parsing documentation for actionview-4.2.5.1
  97. Installing ri documentation for actionview-4.2.5.1
  98. Parsing documentation for actionpack-4.2.5.1
  99. Installing ri documentation for actionpack-4.2.5.1
  100. Parsing documentation for activemodel-4.2.5.1
  101. Installing ri documentation for activemodel-4.2.5.1
  102. Parsing documentation for arel-6.0.3
  103. Installing ri documentation for arel-6.0.3
  104. Parsing documentation for activerecord-4.2.5.1
  105. Installing ri documentation for activerecord-4.2.5.1
  106. Parsing documentation for globalid-0.3.6
  107. Installing ri documentation for globalid-0.3.6
  108. Parsing documentation for activejob-4.2.5.1
  109. Installing ri documentation for activejob-4.2.5.1
  110. Parsing documentation for mime-types-2.99
  111. Installing ri documentation for mime-types-2.99
  112. Parsing documentation for mail-2.6.3
  113. Installing ri documentation for mail-2.6.3
  114. Parsing documentation for actionmailer-4.2.5.1
  115. Installing ri documentation for actionmailer-4.2.5.1
  116. Parsing documentation for thor-0.19.1
  117. Installing ri documentation for thor-0.19.1
  118. Parsing documentation for railties-4.2.5.1
  119. Installing ri documentation for railties-4.2.5.1
  120. Parsing documentation for bundler-1.11.2
  121. Installing ri documentation for bundler-1.11.2
  122. Parsing documentation for concurrent-ruby-1.0.0
  123. Installing ri documentation for concurrent-ruby-1.0.0
  124. Parsing documentation for sprockets-3.5.2
  125. Installing ri documentation for sprockets-3.5.2
  126. Parsing documentation for sprockets-rails-3.0.1
  127. Installing ri documentation for sprockets-rails-3.0.1
  128. Parsing documentation for rails-4.2.5.1
  129. unable to convert "\xFF" from ASCII-8BIT to UTF-8 for guides/assets/images/akshaysurve.jpg, skipping
  130. unable to convert "\x89" from ASCII-8BIT to UTF-8 for guides/assets/images/belongs_to.png, skipping
  131. unable to convert "\xF4" from ASCII-8BIT to UTF-8 for guides/assets/images/book_icon.gif, skipping
  132. unable to convert "\x91" from ASCII-8BIT to UTF-8 for guides/assets/images/bullet.gif, skipping
  133. ...
  134. ...continued on and on...
  135. ...
  136. unable to convert "\x80" from ASCII-8BIT to UTF-8 for guides/assets/images/tab_yellow.gif, skipping
  137. unable to convert "\x89" from ASCII-8BIT to UTF-8 for guides/assets/images/tab_yellow.png, skipping
  138. unable to convert "\xFF" from ASCII-8BIT to UTF-8 for guides/assets/images/vijaydev.jpg, skipping
  139. Installing ri documentation for rails-4.2.5.1
  140. 31 gems installed

但是,它起作用了:

  1. $ rails --version
  2. Rails 4.2.5.1

创建您的应用程序和框架实例

然后,我创建了一个名为blog的新应用程序:

  1. $ rails new blog
  2. create
  3. create README.rdoc
  4. create Rakefile
  5. create config.ru
  6. create .gitignore
  7. create Gemfile
  8. create app
  9. create app/assets/javascripts/application.js
  10. create app/assets/stylesheets/application.css
  11. create app/controllers/application_controller.rb
  12. create app/helpers/application_helper.rb
  13. create app/views/layouts/application.html.erb
  14. create app/assets/images/.keep
  15. create app/mailers/.keep
  16. create app/models/.keep
  17. create app/controllers/concerns/.keep
  18. create app/models/concerns/.keep
  19. create bin
  20. create bin/bundle
  21. create bin/rails
  22. create bin/rake
  23. create bin/setup
  24. create config
  25. create config/routes.rb
  26. create config/application.rb
  27. create config/environment.rb
  28. create config/secrets.yml
  29. create config/environments
  30. create config/environments/development.rb
  31. create config/environments/production.rb
  32. create config/environments/test.rb
  33. create config/initializers
  34. create config/initializers/assets.rb
  35. create config/initializers/backtrace_silencers.rb
  36. create config/initializers/cookies_serializer.rb
  37. create config/initializers/filter_parameter_logging.rb
  38. create config/initializers/inflections.rb
  39. create config/initializers/mime_types.rb
  40. create config/initializers/session_store.rb
  41. create config/initializers/wrap_parameters.rb
  42. create config/locales
  43. create config/locales/en.yml
  44. create config/boot.rb
  45. create config/database.yml
  46. create db
  47. create db/seeds.rb
  48. create lib
  49. create lib/tasks
  50. create lib/tasks/.keep
  51. create lib/assets
  52. create lib/assets/.keep
  53. create log
  54. create log/.keep
  55. create public
  56. create public/404.html
  57. create public/422.html
  58. create public/500.html
  59. create public/favicon.ico
  60. create public/robots.txt
  61. create test/fixtures
  62. create test/fixtures/.keep
  63. create test/controllers
  64. create test/controllers/.keep
  65. create test/mailers
  66. create test/mailers/.keep
  67. create test/models
  68. create test/models/.keep
  69. create test/helpers
  70. create test/helpers/.keep
  71. create test/integration
  72. create test/integration/.keep
  73. create test/test_helper.rb
  74. create tmp/cache
  75. create tmp/cache/assets
  76. create vendor/assets/javascripts
  77. create vendor/assets/javascripts/.keep
  78. create vendor/assets/stylesheets
  79. create vendor/assets/stylesheets/.keep
  80. run bundle install
  81. Your user account isn't allowed to install to the system Rubygems.
  82. You can cancel this installation and run:
  83. bundle install --path vendor/bundle
  84. to install the gems into ./vendor/bundle/, or you can enter your password
  85. and install the bundled gems to Rubygems using sudo.
  86. Password:
  87. Fetching gem metadata from https://rubygems.org/...........
  88. Fetching version metadata from https://rubygems.org/...
  89. Fetching dependency metadata from https://rubygems.org/..
  90. Resolving dependencies....
  91. Rubygems 2.0.14 is not threadsafe, so your gems will be installed one at a time. Upgrade to Rubygems 2.1.0 or higher to enable parallel gem installation.
  92. Installing rake 10.5.0
  93. Using i18n 0.7.0
  94. Installing json 1.8.3 with native extensions
  95. Using minitest 5.8.4
  96. Using thread_safe 0.3.5
  97. Using builder 3.2.2
  98. Using erubis 2.7.0
  99. Using mini_portile2 2.0.0
  100. Using rack 1.6.4
  101. Using mime-types 2.99
  102. Using arel 6.0.3
  103. Installing debug_inspector 0.0.2 with native extensions
  104. Using bundler 1.11.2
  105. Installing byebug 8.2.2 with native extensions
  106. Installing coffee-script-source 1.10.0
  107. Installing execjs 2.6.0
  108. Using thor 0.19.1
  109. Using concurrent-ruby 1.0.0
  110. Installing multi_json 1.11.2
  111. Installing sass 3.4.21
  112. Installing tilt 2.0.2
  113. Installing spring 1.6.3
  114. Installing sqlite3 1.3.11 with native extensions
  115. Installing rdoc 4.2.1
  116. Using tzinfo 1.2.2
  117. Using nokogiri 1.6.7.2
  118. Using rack-test 0.6.3
  119. Using mail 2.6.3
  120. Installing binding_of_caller 0.7.2 with native extensions
  121. Installing coffee-script 2.4.1
  122. Installing uglifier 2.7.2
  123. Using sprockets 3.5.2
  124. Installing sdoc 0.4.1
  125. Using activesupport 4.2.5.1
  126. Using loofah 2.0.3
  127. Using rails-deprecated_sanitizer 1.0.3
  128. Using globalid 0.3.6
  129. Using activemodel 4.2.5.1
  130. Installing jbuilder 2.4.0
  131. Using rails-html-sanitizer 1.0.3
  132. Using rails-dom-testing 1.0.7
  133. Using activejob 4.2.5.1
  134. Using activerecord 4.2.5.1
  135. Using actionview 4.2.5.1
  136. Using actionpack 4.2.5.1
  137. Using actionmailer 4.2.5.1
  138. Using railties 4.2.5.1
  139. Using sprockets-rails 3.0.1
  140. Installing coffee-rails 4.1.1
  141. Installing jquery-rails 4.1.0
  142. Using rails 4.2.5.1
  143. Installing sass-rails 5.0.4
  144. Installing web-console 2.3.0
  145. Installing turbolinks 2.5.3
  146. Bundle complete! 12 Gemfile dependencies, 54 gems now installed.
  147. Use `bundle show [gemname]` to see where a bundled gem is installed.
  148. Post-install message from rdoc:
  149. Depending on your version of ruby, you may need to install ruby rdoc/ri data:
  150. <= 1.8.6 : unsupported
  151. = 1.8.7 : gem install rdoc-data; rdoc-data --install
  152. = 1.9.1 : gem install rdoc-data; rdoc-data --install
  153. >= 1.9.2 : nothing to do! Yay!
  154. run bundle exec spring binstub --all
  155. Warning: You're using Rubygems 2.0.14 with Spring. Upgrade to at least Rubygems 2.1.0 and run `gem pristine --all` for better startup performance.
  156. * bin/rake: spring inserted
  157. * bin/rails: spring inserted

您可以在应用程序中浏览Rails Web应用程序框架目录结构:

  1. $ cd blog
  2. $ ls
  3. Gemfile Rakefile config lib test
  4. Gemfile.lock app config.ru log tmp
  5. README.rdoc bin db public vendor

这是《入门指南》中的一个视图,显示了Rails如何依赖约定而不是配置:

什么是Ruby on Rails目录结构约定

启动您的服务器

最后,您可以使用命令行启动服务器:

  1. $ bin/rails server
  2. Warning: You're using Rubygems 2.0.14 with Spring. Upgrade to at least Rubygems 2.1.0 and run `gem pristine --all` for better startup performance.
  3. => Booting WEBrick
  4. => Rails 4.2.5.1 application starting in development on http://localhost:3000
  5. => Run `rails server -h` for more startup options
  6. => Ctrl-C to shutdown server
  7. [2016-02-05 18:14:05] INFO WEBrick 1.3.1
  8. [2016-02-05 18:14:05] INFO ruby 2.0.0 (2015-04-13) [universal.x86_64-darwin15]
  9. [2016-02-05 18:14:05] INFO WEBrick::HTTPServer#start: pid=30576 port=3000

访问您的申请

在浏览器中,转到http:// localhost:3000 ,您应该看到通用Rails应用程序的默认主页:

什么是新应用程序的Ruby on Rails欢迎页面

我不得不说,我希望所有这些都更加干净。 也就是说,有一些很棒的社区资源可以帮助您成长。

更深入

这是一个免费的GoRails截屏视频的示例,其中覆盖了以上目录中显示的基本框架应用程序体系结构:

Rails Guide本身会引导您完成许多初始步骤:

什么是Ruby on Rails入门指南目录

总体而言,这里有各种各样的书面和截屏教程来指导您。

收盘时

如果您对Ruby on Rails及其社区的质量感兴趣,我希望您发现本介绍对您有所帮助。 也许您甚至尝试了上面的安装。

最近,我对Apple的Swift开源感到很感兴趣,Swift是进入面向对象编程语言领域的激动人心的参与者,类似于Ruby,与Rails具有类似的长期潜力。

过去与Rails合作时,我也一直在挣扎,尤其是在托管和扩展功能方面–我还发现托管它的成本更高。 我对Yii仍然感到最满意,您可以在我们的Envato Tuts + Yii2编程系列中阅读有关内容。 随着PHP 7的出现,基于PHP的编程框架的未来看起来更快,更光明。

如有疑问,请在下面发布。 或者,您可以通过Twitter @reifman与我联系 。 请查看我的Envato Tuts +讲师页面,以查看我编写的其他教程,例如“ 使用社交媒体定位目击者” ,其中着重介绍了Instagram和Twitter的API。

相关链接

翻译自: https://code.tutsplus.com/tutorials/what-is-ruby-on-rails--cms-22077

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/620404
推荐阅读
相关标签
  

闽ICP备14008679号