Download pdf - RoR (Ruby on Rails)

Transcript
Page 1: RoR (Ruby on Rails)

Ruby on Rails

8/01/2015 Jānis Caune

Page 2: RoR (Ruby on Rails)

First things first

Ruby is a programming languageRuby Gems are Ruby packagesRubyGems is a package management framework for RubyRuby on Rails is a Web frameworkRuby on Rails is also a Ruby gem

Page 3: RoR (Ruby on Rails)

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?

Page 4: RoR (Ruby on Rails)

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!

Page 5: RoR (Ruby on Rails)

Some examplesEverything 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 classclass Time def yesterday self - 86400 endendtoday = Time.now # => 2013-09-03 16:09:37 +0300yesterday = today.yesterday # => 2013-09-02 16:09:37 +0300

Page 6: RoR (Ruby on Rails)

Some examplesBlocks 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 lineend

Page 7: RoR (Ruby on Rails)

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

Page 8: RoR (Ruby on Rails)

RoR components/app/ /app/assets/ /app/controllers/ /app/helpers/ /app/mailers/ /app/models/ /app/views//bin/

/config//db//lib//log//public/ /public/assets/ /public/images/ /public/javascripts/ /public/stylesheets/ /public/system/

/test//tmp//vendor//Gemfile

Page 9: RoR (Ruby on Rails)

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

Page 10: RoR (Ruby on Rails)

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..

Page 11: RoR (Ruby on Rails)

How to get started?Ways to run RoR apps:

● built in webserver - rails server

● Passenger module for Apache/Nginx (recommended)

● Unicorn webserver

● Puma webserver

Page 12: RoR (Ruby on Rails)

How do I build an app?Easy!rails new fabulousapp # create the app, done!cd fabulousapprails g scaffold article # let’s have all at once… edit migration, define fields …rake db:migrate # oh, right, no DB definedrake routes # scaffold is good for you (add default, though)

At this point, you can launch your app!

Page 13: RoR (Ruby on Rails)

Articles need commentsEasy, 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...

Page 14: RoR (Ruby on Rails)

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...

Page 15: RoR (Ruby on Rails)

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>

Page 16: RoR (Ruby on Rails)

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} %>

Page 17: RoR (Ruby on Rails)

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/

Page 18: RoR (Ruby on Rails)

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

Page 19: RoR (Ruby on Rails)

Is there a gem for …?

● Most likely, yes.● AND, you get to rewrite them, if needed.

Page 20: RoR (Ruby on Rails)

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

Page 21: RoR (Ruby on Rails)

Doing > Listening! Good luck!

Questions?


Recommended