图片来源: 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具有很高的交易率和可扩展性挑战。
在本教程中,我将回答问题“什么是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,后者用于与数据库进行交互。
Rails的创始人汉森(Hansson)在名为Ruby on Rails的框架背后提供了原理性神学。
Ruby on Rails学说
Rails原则包含八个原则:
- 优化程序员的幸福感 :Ruby尽可能满足程序员的需求。
- 约定优于配置 :选择标准的,平台范围的默认选项可以使程序员更快速,更轻松地开始新项目。
- 菜单是omakase :Ruby on Rails背后的团队已经选择了最好的工具和方法,每个人都将更高兴使用它们。
- 没有一个范例 :Rails最终为各种场合提供了灵活的解决方案。
- 高尚美丽的代码 :主要关注美观和简单。
- 价值集成系统 :一种方向,旨在完全了解和凝聚构建应用程序和解决方案所需的所有工具。
- 超越稳定性的进步 :愿意冒险将平台向前推进。
- 推开一个大帐篷 :欢迎广大参与者和他们的声音来设定愿景。
Rails的主要成就是围绕围绕编程和程序员本质的一系列异端思想团结并培养一个强大的部落。 —大卫·海尼迈尔·汉森
以下是当前主要的Rails贡献者(创始人David Hansson在左上方):
他们确实有一个热情的社区:
- Ruby on Rails:讨论邮件列表
- Ruby on Rails StackOverflow Q&A标签
- irc.freenode.net上的#rubyonrails IRC频道
- 年度RailsConf会议,用于现实世界的聚会
也许您有兴趣进一步学习,所以这里是开始的方法。
Ruby on Rails入门
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:
- $ ruby -v
- ruby 2.0.0p645 (2015-04-13 revision 50299) [universal.x86_64-darwin15]
安装Rails框架
我的Rails安装大约花了10到15分钟(请耐心等待):
- $ sudo gem install rails
- Password:
- Fetching: thread_safe-0.3.5.gem (100%)
- Successfully installed thread_safe-0.3.5
- Fetching: tzinfo-1.2.2.gem (100%)
- Successfully installed tzinfo-1.2.2
- Fetching: minitest-5.8.4.gem (100%)
- Successfully installed minitest-5.8.4
- Fetching: activesupport-4.2.5.1.gem (100%)
- Successfully installed activesupport-4.2.5.1
- Fetching: rack-1.6.4.gem (100%)
- Successfully installed rack-1.6.4
- Fetching: rack-test-0.6.3.gem (100%)
- Successfully installed rack-test-0.6.3
- Fetching: mini_portile2-2.0.0.gem (100%)
- Successfully installed mini_portile2-2.0.0
- Fetching: nokogiri-1.6.7.2.gem (100%)
- Building native extensions. This could take a while...
- Successfully installed nokogiri-1.6.7.2
- Fetching: loofah-2.0.3.gem (100%)
- Successfully installed loofah-2.0.3
- Fetching: rails-html-sanitizer-1.0.3.gem (100%)
- Successfully installed rails-html-sanitizer-1.0.3
- Fetching: rails-deprecated_sanitizer-1.0.3.gem (100%)
- Successfully installed rails-deprecated_sanitizer-1.0.3
- Fetching: rails-dom-testing-1.0.7.gem (100%)
- Successfully installed rails-dom-testing-1.0.7
- Fetching: builder-3.2.2.gem (100%)
- Successfully installed builder-3.2.2
- Fetching: erubis-2.7.0.gem (100%)
- Successfully installed erubis-2.7.0
- Fetching: actionview-4.2.5.1.gem (100%)
- Successfully installed actionview-4.2.5.1
- Fetching: actionpack-4.2.5.1.gem (100%)
- Successfully installed actionpack-4.2.5.1
- Fetching: activemodel-4.2.5.1.gem (100%)
- Successfully installed activemodel-4.2.5.1
- Fetching: arel-6.0.3.gem (100%)
- Successfully installed arel-6.0.3
- Fetching: activerecord-4.2.5.1.gem (100%)
- Successfully installed activerecord-4.2.5.1
- Fetching: globalid-0.3.6.gem (100%)
- Successfully installed globalid-0.3.6
- Fetching: activejob-4.2.5.1.gem (100%)
- Successfully installed activejob-4.2.5.1
- Fetching: mime-types-2.99.gem (100%)
- Successfully installed mime-types-2.99
- Fetching: mail-2.6.3.gem (100%)
- Successfully installed mail-2.6.3
- Fetching: actionmailer-4.2.5.1.gem (100%)
- Successfully installed actionmailer-4.2.5.1
- Fetching: thor-0.19.1.gem (100%)
- Successfully installed thor-0.19.1
- Fetching: railties-4.2.5.1.gem (100%)
- Successfully installed railties-4.2.5.1
- Fetching: bundler-1.11.2.gem (100%)
- Successfully installed bundler-1.11.2
- Fetching: concurrent-ruby-1.0.0.gem (100%)
- Successfully installed concurrent-ruby-1.0.0
- Fetching: sprockets-3.5.2.gem (100%)
- Successfully installed sprockets-3.5.2
- Fetching: sprockets-rails-3.0.1.gem (100%)
- Successfully installed sprockets-rails-3.0.1
- Fetching: rails-4.2.5.1.gem (100%)
- Successfully installed rails-4.2.5.1
- Parsing documentation for thread_safe-0.3.5
- Installing ri documentation for thread_safe-0.3.5
- Parsing documentation for tzinfo-1.2.2
- Installing ri documentation for tzinfo-1.2.2
- Parsing documentation for minitest-5.8.4
- Installing ri documentation for minitest-5.8.4
- Parsing documentation for activesupport-4.2.5.1
- unable to convert "\x84" from ASCII-8BIT to UTF-8 for lib/active_support/values/unicode_tables.dat, skipping
- Installing ri documentation for activesupport-4.2.5.1
- Parsing documentation for rack-1.6.4
- Installing ri documentation for rack-1.6.4
- Parsing documentation for rack-test-0.6.3
- Installing ri documentation for rack-test-0.6.3
- Parsing documentation for mini_portile2-2.0.0
- Installing ri documentation for mini_portile2-2.0.0
- Parsing documentation for nokogiri-1.6.7.2
- unable to convert "\xCA" from ASCII-8BIT to UTF-8 for lib/nokogiri/nokogiri.bundle, skipping
- Installing ri documentation for nokogiri-1.6.7.2
- Parsing documentation for loofah-2.0.3
- Installing ri documentation for loofah-2.0.3
- Parsing documentation for rails-html-sanitizer-1.0.3
- Installing ri documentation for rails-html-sanitizer-1.0.3
- Parsing documentation for rails-deprecated_sanitizer-1.0.3
- Installing ri documentation for rails-deprecated_sanitizer-1.0.3
- Parsing documentation for rails-dom-testing-1.0.7
- Installing ri documentation for rails-dom-testing-1.0.7
- Parsing documentation for builder-3.2.2
- Installing ri documentation for builder-3.2.2
- Parsing documentation for erubis-2.7.0
- Installing ri documentation for erubis-2.7.0
- Parsing documentation for actionview-4.2.5.1
- Installing ri documentation for actionview-4.2.5.1
- Parsing documentation for actionpack-4.2.5.1
- Installing ri documentation for actionpack-4.2.5.1
- Parsing documentation for activemodel-4.2.5.1
- Installing ri documentation for activemodel-4.2.5.1
- Parsing documentation for arel-6.0.3
- Installing ri documentation for arel-6.0.3
- Parsing documentation for activerecord-4.2.5.1
- Installing ri documentation for activerecord-4.2.5.1
- Parsing documentation for globalid-0.3.6
- Installing ri documentation for globalid-0.3.6
- Parsing documentation for activejob-4.2.5.1
- Installing ri documentation for activejob-4.2.5.1
- Parsing documentation for mime-types-2.99
- Installing ri documentation for mime-types-2.99
- Parsing documentation for mail-2.6.3
- Installing ri documentation for mail-2.6.3
- Parsing documentation for actionmailer-4.2.5.1
- Installing ri documentation for actionmailer-4.2.5.1
- Parsing documentation for thor-0.19.1
- Installing ri documentation for thor-0.19.1
- Parsing documentation for railties-4.2.5.1
- Installing ri documentation for railties-4.2.5.1
- Parsing documentation for bundler-1.11.2
- Installing ri documentation for bundler-1.11.2
- Parsing documentation for concurrent-ruby-1.0.0
- Installing ri documentation for concurrent-ruby-1.0.0
- Parsing documentation for sprockets-3.5.2
- Installing ri documentation for sprockets-3.5.2
- Parsing documentation for sprockets-rails-3.0.1
- Installing ri documentation for sprockets-rails-3.0.1
- Parsing documentation for rails-4.2.5.1
- unable to convert "\xFF" from ASCII-8BIT to UTF-8 for guides/assets/images/akshaysurve.jpg, skipping
- unable to convert "\x89" from ASCII-8BIT to UTF-8 for guides/assets/images/belongs_to.png, skipping
- unable to convert "\xF4" from ASCII-8BIT to UTF-8 for guides/assets/images/book_icon.gif, skipping
- unable to convert "\x91" from ASCII-8BIT to UTF-8 for guides/assets/images/bullet.gif, skipping
- ...
- ...continued on and on...
- ...
- unable to convert "\x80" from ASCII-8BIT to UTF-8 for guides/assets/images/tab_yellow.gif, skipping
- unable to convert "\x89" from ASCII-8BIT to UTF-8 for guides/assets/images/tab_yellow.png, skipping
- unable to convert "\xFF" from ASCII-8BIT to UTF-8 for guides/assets/images/vijaydev.jpg, skipping
- Installing ri documentation for rails-4.2.5.1
- 31 gems installed
但是,它起作用了:
- $ rails --version
- Rails 4.2.5.1
创建您的应用程序和框架实例
然后,我创建了一个名为blog的新应用程序:
- $ rails new blog
- create
- create README.rdoc
- create Rakefile
- create config.ru
- create .gitignore
- create Gemfile
- create app
- create app/assets/javascripts/application.js
- create app/assets/stylesheets/application.css
- create app/controllers/application_controller.rb
- create app/helpers/application_helper.rb
- create app/views/layouts/application.html.erb
- create app/assets/images/.keep
- create app/mailers/.keep
- create app/models/.keep
- create app/controllers/concerns/.keep
- create app/models/concerns/.keep
- create bin
- create bin/bundle
- create bin/rails
- create bin/rake
- create bin/setup
- create config
- create config/routes.rb
- create config/application.rb
- create config/environment.rb
- create config/secrets.yml
- create config/environments
- create config/environments/development.rb
- create config/environments/production.rb
- create config/environments/test.rb
- create config/initializers
- create config/initializers/assets.rb
- create config/initializers/backtrace_silencers.rb
- create config/initializers/cookies_serializer.rb
- create config/initializers/filter_parameter_logging.rb
- create config/initializers/inflections.rb
- create config/initializers/mime_types.rb
- create config/initializers/session_store.rb
- create config/initializers/wrap_parameters.rb
- create config/locales
- create config/locales/en.yml
- create config/boot.rb
- create config/database.yml
- create db
- create db/seeds.rb
- create lib
- create lib/tasks
- create lib/tasks/.keep
- create lib/assets
- create lib/assets/.keep
- create log
- create log/.keep
- create public
- create public/404.html
- create public/422.html
- create public/500.html
- create public/favicon.ico
- create public/robots.txt
- create test/fixtures
- create test/fixtures/.keep
- create test/controllers
- create test/controllers/.keep
- create test/mailers
- create test/mailers/.keep
- create test/models
- create test/models/.keep
- create test/helpers
- create test/helpers/.keep
- create test/integration
- create test/integration/.keep
- create test/test_helper.rb
- create tmp/cache
- create tmp/cache/assets
- create vendor/assets/javascripts
- create vendor/assets/javascripts/.keep
- create vendor/assets/stylesheets
- create vendor/assets/stylesheets/.keep
- run bundle install
-
-
- Your user account isn't allowed to install to the system Rubygems.
- You can cancel this installation and run:
- bundle install --path vendor/bundle
- to install the gems into ./vendor/bundle/, or you can enter your password
- and install the bundled gems to Rubygems using sudo.
- Password:
- Fetching gem metadata from https://rubygems.org/...........
- Fetching version metadata from https://rubygems.org/...
- Fetching dependency metadata from https://rubygems.org/..
- Resolving dependencies....
- 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.
- Installing rake 10.5.0
- Using i18n 0.7.0
- Installing json 1.8.3 with native extensions
- Using minitest 5.8.4
- Using thread_safe 0.3.5
- Using builder 3.2.2
- Using erubis 2.7.0
- Using mini_portile2 2.0.0
- Using rack 1.6.4
- Using mime-types 2.99
- Using arel 6.0.3
- Installing debug_inspector 0.0.2 with native extensions
- Using bundler 1.11.2
- Installing byebug 8.2.2 with native extensions
- Installing coffee-script-source 1.10.0
- Installing execjs 2.6.0
- Using thor 0.19.1
- Using concurrent-ruby 1.0.0
- Installing multi_json 1.11.2
- Installing sass 3.4.21
- Installing tilt 2.0.2
- Installing spring 1.6.3
- Installing sqlite3 1.3.11 with native extensions
- Installing rdoc 4.2.1
- Using tzinfo 1.2.2
- Using nokogiri 1.6.7.2
- Using rack-test 0.6.3
- Using mail 2.6.3
- Installing binding_of_caller 0.7.2 with native extensions
- Installing coffee-script 2.4.1
- Installing uglifier 2.7.2
- Using sprockets 3.5.2
- Installing sdoc 0.4.1
- Using activesupport 4.2.5.1
- Using loofah 2.0.3
- Using rails-deprecated_sanitizer 1.0.3
- Using globalid 0.3.6
- Using activemodel 4.2.5.1
- Installing jbuilder 2.4.0
- Using rails-html-sanitizer 1.0.3
- Using rails-dom-testing 1.0.7
- Using activejob 4.2.5.1
- Using activerecord 4.2.5.1
- Using actionview 4.2.5.1
- Using actionpack 4.2.5.1
- Using actionmailer 4.2.5.1
- Using railties 4.2.5.1
- Using sprockets-rails 3.0.1
- Installing coffee-rails 4.1.1
- Installing jquery-rails 4.1.0
- Using rails 4.2.5.1
- Installing sass-rails 5.0.4
- Installing web-console 2.3.0
- Installing turbolinks 2.5.3
- Bundle complete! 12 Gemfile dependencies, 54 gems now installed.
- Use `bundle show [gemname]` to see where a bundled gem is installed.
- Post-install message from rdoc:
- Depending on your version of ruby, you may need to install ruby rdoc/ri data:
- <= 1.8.6 : unsupported
- = 1.8.7 : gem install rdoc-data; rdoc-data --install
- = 1.9.1 : gem install rdoc-data; rdoc-data --install
- >= 1.9.2 : nothing to do! Yay!
- run bundle exec spring binstub --all
- 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.
- * bin/rake: spring inserted
- * bin/rails: spring inserted
您可以在应用程序中浏览Rails Web应用程序框架目录结构:
- $ cd blog
- $ ls
- Gemfile Rakefile config lib test
- Gemfile.lock app config.ru log tmp
- README.rdoc bin db public vendor
这是《入门指南》中的一个视图,显示了Rails如何依赖约定而不是配置:
启动您的服务器
最后,您可以使用命令行启动服务器:
- $ bin/rails server
- 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.
- => Booting WEBrick
- => Rails 4.2.5.1 application starting in development on http://localhost:3000
- => Run `rails server -h` for more startup options
- => Ctrl-C to shutdown server
- [2016-02-05 18:14:05] INFO WEBrick 1.3.1
- [2016-02-05 18:14:05] INFO ruby 2.0.0 (2015-04-13) [universal.x86_64-darwin15]
- [2016-02-05 18:14:05] INFO WEBrick::HTTPServer#start: pid=30576 port=3000
访问您的申请
在浏览器中,转到http:// localhost:3000 ,您应该看到通用Rails应用程序的默认主页:
我不得不说,我希望所有这些都更加干净。 也就是说,有一些很棒的社区资源可以帮助您成长。
更深入
这是一个免费的GoRails截屏视频的示例,其中覆盖了以上目录中显示的基本框架应用程序体系结构:
Rails Guide本身会引导您完成许多初始步骤:
总体而言,这里有各种各样的书面和截屏教程来指导您。
收盘时
如果您对Ruby on Rails及其社区的质量感兴趣,我希望您发现本介绍对您有所帮助。 也许您甚至尝试了上面的安装。
最近,我对Apple的Swift开源感到很感兴趣,Swift是进入面向对象编程语言领域的激动人心的参与者,类似于Ruby,与Rails具有类似的长期潜力。
过去与Rails合作时,我也一直在挣扎,尤其是在托管和扩展功能方面–我还发现托管它的成本更高。 我对Yii仍然感到最满意,您可以在我们的Envato Tuts + Yii2编程系列中阅读有关内容。 随着PHP 7的出现,基于PHP的编程框架的未来看起来更快,更光明。
如有疑问,请在下面发布。 或者,您可以通过Twitter @reifman与我联系 。 请查看我的Envato Tuts +讲师页面,以查看我编写的其他教程,例如“ 使用社交媒体定位目击者” ,其中着重介绍了Instagram和Twitter的API。
相关链接
- Ruby on Rails
- Ruby on Rails(维基百科)
- 在Twitter上关注@Rails
- 订阅本周的Rails
- Go Rails(截屏视频等)
- Ruby编程语言(维基百科)
- 模型视图控制器(Wikipedia)
- RailsConf
翻译自: https://code.tutsplus.com/tutorials/what-is-ruby-on-rails--cms-22077