Introduction to Ruby- on-Rails Evgeniy Hinyuk Ruby-developer Github Twitter
Who am I? Photo taken: 1.07.2016 self-educated junior ruby-developer
Now
RAILS
What is Rails? • Ruby on Rails is an extremely productive web application framework written in Ruby by David Heinemeier Hansson in 2005. • You could develop a web application at least ten times faster with Rails than you could with a typical Java framework. • An open source Ruby framework for developing database-backed web applications. • No compilation phase required. • Full-stack framework. • Convention over Configuration
Full-stack framework. •Includes everything needed to create a database-driven web application, using the Model-View-Controller pattern. •Being a full-stack framework means all the layers are built to work seamlessly together with less code. •Requires fewer lines of code than other frameworks.
MVC
Convention over Configuration •Rails shuns configuration files in favor of conventions, reflection, and dynamic runtime extensions. •Your application code and your running database already contain everything that Rails needs to know! 

Rails Strengths •Ruby •Metaprogramming •DRY •Active Record •Convention over Configuration •Scaffolding •Built-in testing •Three environments •Fun

Rails installation
Rails server
App directory structure • app - main app directory • bin - rails needed scripts • config - configurations for environment, routes, etc. • db - schema.db, migrations • lib - folder for code not included in main app (like your own gems) • log - log files • public - assets for public access, 404 page etc. • test(spec) - test files • tmp - place for temporary files • vendor -folder used for third-party libraries.
Gemfile A Gemfile is a file which is used for describing gem dependencies for Ruby programs. What is Gemfile?
Scaffolding
Development Workflow • Use the rails command to create the basic skeleton of the application. • Create a database on the MySQL server to hold your data. • Configure the application to know where your database is located and the login credentials for it. • Create Rails Active Records (Models), because they are the business objects you'll be working with in your controllers. • Generate Migrations that simplify the creating and maintaining of database tables and columns. • Write Controller Code to put a life in your application. • Create Views to present your data through User Interface. us start with creating our library application.
Rails and DB
ActiveRecord • Rails Active Record is the Object/Relational Mapping (ORM) layer supplied with Rails. • Each Active Record object has CRUD methods for database access
Associations Active Record supports three types of associations: • One-to-one (has_one); • One-to-many (has_many); • Many-to-many (has_and_belongs_to_many, polymorphic);
Model in DB • Each entity (such as book) gets a table in the database named after it, but in the plural (books). •  Each such entity-matching table has a field called id, which contains a unique integer for each record inserted into the table. • Given entity x and entity y, if entity y belongs to entity x, then table y has a field called x_id. • The bulk of the fields in any table store the values for that entity's simple properties (anything that's a number or a string).
Validations The implementation of validations is done in a Rails model. The data you are entering into the database is defined in the actual Rails model, so it only makes sense to define what valid data entails in the same location. Also you can provide custom validations.
Migrations Rails Migration allows you to use Ruby to define changes to your database schema, making it possible to use a version control system to keep things synchronized with the actual code.
Migrations
Routes The routing module provides URL rewriting in native Ruby. It's a way to redirect incoming requests to controllers and actions. Best of all, Rails' Routing works with any web server. Routes are defined in: app/config/routes.rb
` Rails named routes starts with HTTP verb. It supports all the verbs such as: GET, POST, PUTPATCH, DELETE.
Routes Also rails have a special route for root path. You can match any url to any action.
Resourceful Routes
Nested Routes
Controller The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model.
Controller
CRUD
Strong params It provides an interface for protecting attributes from end- user assignment. This makes Action Controller parameters forbidden to be used in Active Model mass assignment until they have been whitelisted.
Action View In Rails, web requests are handled by Action Controller and Action View. Typically, Action Controller is concerned with communicating with the database and performing CRUD actions where necessary. Action View is then responsible for compiling the response. Action View templates are written using embedded Ruby in tags mingled with HTML.
Layout Use multiple layouts
Helpers The Rails framework provides a large number of helpers for working with assets, dates, forms, numbers and model objects, to name a few. These helpers are available to all templates by default. In addition to using the standard template helpers provided, creating custom helpers to extract complicated logic or reusable functionality is strongly encouraged.
Rails advanced A lot of USEFULL gems
Testing How we test rails app • Unit tests • Functional tests • Integration tests • Rspec • Factory Girl • Cucumber • Capybara • Minitest …
Books 1. Ruby on Rails tutorial 2. Well-Grounded Rubyist 3. Rails 4 Way
Conclusion •Rails is good for prototyping and quick development •Rails is fun! •Rails is full-stack framework based on ruby
Thank you for your listening
Questions?
Homework Write a simple web app which allows you to create an event(bbq, pizza-day, or etc.). Add credentials to it (place, date, etc…). Add two roles, admin, and attendee. Admin can add an event, delete event, add a description, edit an event, add comments to the event. The attendee can join an event, can leave an event, can add comments to the event. You will need to cover it by your preferred test framework and deploy to Heroku.

Lecture #5 Introduction to rails

  • 1.
    Introduction to Ruby- on-Rails EvgeniyHinyuk Ruby-developer Github Twitter
  • 2.
    Who am I? Phototaken: 1.07.2016 self-educated junior ruby-developer
  • 3.
  • 4.
  • 5.
    What is Rails? •Ruby on Rails is an extremely productive web application framework written in Ruby by David Heinemeier Hansson in 2005. • You could develop a web application at least ten times faster with Rails than you could with a typical Java framework. • An open source Ruby framework for developing database-backed web applications. • No compilation phase required. • Full-stack framework. • Convention over Configuration
  • 6.
    Full-stack framework. •Includes everythingneeded to create a database-driven web application, using the Model-View-Controller pattern. •Being a full-stack framework means all the layers are built to work seamlessly together with less code. •Requires fewer lines of code than other frameworks.
  • 7.
  • 8.
    Convention over Configuration •Railsshuns configuration files in favor of conventions, reflection, and dynamic runtime extensions. •Your application code and your running database already contain everything that Rails needs to know! 

  • 9.
    Rails Strengths •Ruby •Metaprogramming •DRY •Active Record •Conventionover Configuration •Scaffolding •Built-in testing •Three environments •Fun

  • 10.
  • 11.
  • 12.
    App directory structure •app - main app directory • bin - rails needed scripts • config - configurations for environment, routes, etc. • db - schema.db, migrations • lib - folder for code not included in main app (like your own gems) • log - log files • public - assets for public access, 404 page etc. • test(spec) - test files • tmp - place for temporary files • vendor -folder used for third-party libraries.
  • 13.
    Gemfile A Gemfile isa file which is used for describing gem dependencies for Ruby programs. What is Gemfile?
  • 14.
  • 15.
    Development Workflow • Usethe rails command to create the basic skeleton of the application. • Create a database on the MySQL server to hold your data. • Configure the application to know where your database is located and the login credentials for it. • Create Rails Active Records (Models), because they are the business objects you'll be working with in your controllers. • Generate Migrations that simplify the creating and maintaining of database tables and columns. • Write Controller Code to put a life in your application. • Create Views to present your data through User Interface. us start with creating our library application.
  • 16.
  • 17.
    ActiveRecord • Rails ActiveRecord is the Object/Relational Mapping (ORM) layer supplied with Rails. • Each Active Record object has CRUD methods for database access
  • 18.
    Associations Active Record supportsthree types of associations: • One-to-one (has_one); • One-to-many (has_many); • Many-to-many (has_and_belongs_to_many, polymorphic);
  • 19.
    Model in DB •Each entity (such as book) gets a table in the database named after it, but in the plural (books). •  Each such entity-matching table has a field called id, which contains a unique integer for each record inserted into the table. • Given entity x and entity y, if entity y belongs to entity x, then table y has a field called x_id. • The bulk of the fields in any table store the values for that entity's simple properties (anything that's a number or a string).
  • 20.
    Validations The implementation ofvalidations is done in a Rails model. The data you are entering into the database is defined in the actual Rails model, so it only makes sense to define what valid data entails in the same location. Also you can provide custom validations.
  • 21.
    Migrations Rails Migration allowsyou to use Ruby to define changes to your database schema, making it possible to use a version control system to keep things synchronized with the actual code.
  • 22.
  • 23.
    Routes The routing module providesURL rewriting in native Ruby. It's a way to redirect incoming requests to controllers and actions. Best of all, Rails' Routing works with any web server. Routes are defined in: app/config/routes.rb
  • 24.
    ` Rails named routesstarts with HTTP verb. It supports all the verbs such as: GET, POST, PUTPATCH, DELETE.
  • 25.
    Routes Also rails havea special route for root path. You can match any url to any action.
  • 26.
  • 27.
  • 28.
    Controller The Rails controlleris the logical center of your application. It coordinates the interaction between the user, the views, and the model.
  • 29.
  • 30.
  • 31.
    Strong params It providesan interface for protecting attributes from end- user assignment. This makes Action Controller parameters forbidden to be used in Active Model mass assignment until they have been whitelisted.
  • 32.
    Action View In Rails,web requests are handled by Action Controller and Action View. Typically, Action Controller is concerned with communicating with the database and performing CRUD actions where necessary. Action View is then responsible for compiling the response. Action View templates are written using embedded Ruby in tags mingled with HTML.
  • 33.
  • 34.
    Helpers The Rails frameworkprovides a large number of helpers for working with assets, dates, forms, numbers and model objects, to name a few. These helpers are available to all templates by default. In addition to using the standard template helpers provided, creating custom helpers to extract complicated logic or reusable functionality is strongly encouraged.
  • 35.
    Rails advanced A lotof USEFULL gems
  • 36.
    Testing How we testrails app • Unit tests • Functional tests • Integration tests • Rspec • Factory Girl • Cucumber • Capybara • Minitest …
  • 37.
    Books 1. Ruby onRails tutorial 2. Well-Grounded Rubyist 3. Rails 4 Way
  • 38.
    Conclusion •Rails is goodfor prototyping and quick development •Rails is fun! •Rails is full-stack framework based on ruby
  • 39.
    Thank you foryour listening
  • 40.
  • 41.
    Homework Write a simpleweb app which allows you to create an event(bbq, pizza-day, or etc.). Add credentials to it (place, date, etc…). Add two roles, admin, and attendee. Admin can add an event, delete event, add a description, edit an event, add comments to the event. The attendee can join an event, can leave an event, can add comments to the event. You will need to cover it by your preferred test framework and deploy to Heroku.