2. First things first
Ruby is a programming language
Ruby Gems are Ruby packages
RubyGems is a package management
framework for Ruby
Ruby on Rails is a Web framework
Ruby on Rails is also a Ruby gem
3. Why Ruby?
● Designed in mid-1990s by Yukihiro Matsamuto
● "I hope to see Ruby help every programmer in the world
to be productive, and to enjoy programming, and to be
happy. That is the primary purpose of Ruby language."
● Goal is Very Nice, but what’s Ruby?
4. What’s Ruby?
Ruby is dynamic, reflective, object-oriented general
purpose programming language.
Also, it is very user friendly(after some time).
Also, let’s see examples and welcome to check out
Wikipedia!
5. Some examples
Everything is an object
-199.abs # => 199
"ice is nice".length # => 11
"ruby is cool.".index("u") # => 1
"Nice Day Isn't It?".downcase.split("").uniq.sort.join # => " '?acdeinsty"
Classes are never closed
# re-open Ruby's Time class
class Time
def yesterday
self - 86400
end
end
today = Time.now # => 2013-09-03 16:09:37 +0300
yesterday = today.yesterday # => 2013-09-02 16:09:37 +0300
6. Some examples
Blocks and iterators
{ puts "Hello, World!" } # note the braces
# or:
do
puts "Hello, World!"
end
array.each {|item| puts item }
array.each_index {|index| puts "#{index}: #{array[index]}" }
File.readlines('file.txt').each do |line|
puts line
end
7. OK, what’s RoR?
● full stack framework
● makes use of
○ Model-View-Controller
○ Don’t Repeat Yourself
○ Active Record
○ RESTful routes
○ Fat Models Skinny Controllers
● first released on 2004, as extract from Basecamp
9. RoR tools
● rails itself:
○ rails new
○ rails g (scaffold|model|controller|migration|...)
○ rails server
● rake, the Ruby make (for running tasks defined by RoR and you):
○ rake db:migrate
○ rake assets:precompile
○ rake somelib:sometask
● bundler, takes care of project specific gems, specified in Gemfile:
○ bundle install
10. How to get started?
● Install RVM - much recommended!
● Install Ruby using RVM
● Install RubyGems using RVM
● Install your first gem - Rails (gem install smth)
● Now you have tools to start developing - but you need
to run the app somehow..
11. How to get started?
Ways to run RoR apps:
● built in webserver - rails server
● Passenger module for Apache/Nginx (recommended)
● Unicorn webserver
● Puma webserver
12. How do I build an app?
Easy!
rails new fabulousapp # create the app, done!
cd fabulousapp
rails g scaffold article # let’s have all at once
… edit migration, define fields …
rake db:migrate # oh, right, no DB defined
rake routes # scaffold is good for you (add default, though)
At this point, you can launch your app!
13. Articles need comments
Easy, just:
● create new scaffold: rails g comment
● add relation:
○ comment - belongs_to :article
○ article - has_many :comments (see plural form?)
● get the comments, e.g. in controller:
class ArticlesController < ApplicationController
def show
@article = Article.find(params[:id])
@comments = @article.comments
end
...
14. We want to see comments!
Each controller action should have an associated view (unless configured
otherwise):
so, for ArticlesController show action we’d have:
app/views/articles/show.html.erb
Response format can also be changed:
class ArticlesController < ApplicationController
def lazy_load
@article = Article.find(params[:id])
@comments = @article.comments
respond_with :js # Will look for app/views/articles/lazy_load.js.erb
end
...
15. We want to see comments!
Views in Rails are layout based
(controller defines layout):
app/views/layouts/application.html.erb
Here we see how:
- stylesheets and javascripts are
included
- what variable tag looks like
- where does controller response go
to
<!DOCTYPE html>
<html>
<head>
<title>Fabulousapp</title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-
turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
16. Still no comments!
Views can consist of single template:
<div class="wrapper">
<div class="center_content">
<%= article.content %>
<% if @comments.any? %>
<div class="comments_placeholder">
….
Or, they can call other views as well:
….
<% if @comments.any? %>
<%= render partial: 'comment_form', locals: {article: @article} %>
17. This is ugly!
Rails use SCSS and CoffeeScript for styles and frontend scripts. One can
always fall back to vanilla CSS and JS. Remember application.html.erb?
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
This will look for app/assets/javascripts/application.js
You can define JS/CSS on action scope, as controller can define layout.
Images referenced in CSS is stored in app/assets/images/
18. What about bad guys?
● for certain actions you can set before_filter:
class ArticlesController < ApplicationController
before_filter :require_uber_user, :only => [:delete]
def require_uber_user
current_user.uber_user
end
● CSRF token support by default
● few good auth gems available
19. Is there a gem for …?
● Most likely, yes.
● AND, you get to rewrite them, if needed.
20. My changes don’t work!
Unless development environment variable is set up, Rails
app will be run in it’s compiled state and won’t care about
code or assets changes.
To avoid it:
- set the variable already, OR
- touch tmp/restart.txt
- rake assets:precompile