of 21 /21
Ruby on Rails 8/01/2015 Jānis Caune

RoR (Ruby on Rails)

Embed Size (px)

Text of RoR (Ruby on Rails)

  • Ruby on Rails

    8/01/2015 Jnis Caune

  • 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

  • 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 whats Ruby?

  • Whats Ruby?Ruby is dynamic, reflective, object-oriented general purpose programming language.

    Also, it is very user friendly(after some time).

    Also, lets see examples and welcome to check out Wikipedia!

  • 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

  • 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

  • OK, whats RoR? full stack framework makes use of

    Model-View-Controller Dont Repeat Yourself Active Record RESTful routes Fat Models Skinny Controllers

    first released on 2004, as extract from Basecamp

  • 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/


  • 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

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

  • How to get started?Ways to run RoR apps:

    built in webserver - rails server

    Passenger module for Apache/Nginx (recommended)

    Unicorn webserver

    Puma webserver

  • How do I build an app?Easy!rails new fabulousapp # create the app, done!cd fabulousapprails g scaffold article # lets 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!

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

  • We want to see comments!Each controller action should have an associated view (unless configured otherwise):so, for ArticlesController show action wed have:


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

  • 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


    Fabulousapp true %> true %>

  • Still no comments!Views can consist of single template:


    Or, they can call other views as well:.

  • 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? 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/

  • 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

  • Is there a gem for ?

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

  • My changes dont work!Unless development environment variable is set up, Rails app will be run in its compiled state and wont care about code or assets changes.To avoid it:- set the variable already, OR- touch tmp/restart.txt- rake assets:precompile

  • Doing > Listening! Good luck!