$30 off During Our Annual Pro Sale. View Details »

Rails Internals - Saroj Maharjan

August 30, 2014

Rails Internals - Saroj Maharjan

Session on #DevMeetup (Aug. 30, 2014), Kathmandu, Nepal.


August 30, 2014

More Decks by zoras

Other Decks in Technology


  1. Rails Internals Saroj Maharjan (zoras) @zoraslapen

  2. #RailsTurns10

  3. Ruby on Rails is_a? • Open-source • On Github •

    git clone rails/rails # using hub • 100% Ruby • No C extensions!
  4. This Talk • The Request Handling • How Rails Works

    • What Rails Gives us
  5. The Request Handling

  6. • a Ruby Webserver Interface • an interface between a

    HTTP server, and a Ruby application • a standard language between those two parts
  7. Rack • When the HTTP Server receives a HTTP request,

    it will create a hash called env • A Rack application is any ruby object that can respond to the call method, it can be a lambda, or an instance of a class that defines a call method. ! • rackup
  8. Rack Middlewares • Rack is a two-ways pipeline • add

    some code in the pipeline to modify either the request or the response • A middleware is just like a Rack app, except that you construct it with another Rack application.
  9. None
  10. How Rails Works

  11. How Rails server boots % rails server Rails.root/bin/rails

  12. Rails application • Rails is a Rack application like the

    one we just saw • config.ru ! ! • Rails.application is an attribute accessor defined in railties/lib/rails.rb • config/environment.rb will Initialize your rails application with
  13. Rails Middleware • When instantiating a rails app, Rails.application is

    set in railties/lib/rails/ application.rb ! ! ! ! ! • Now that we have a Rails.application, rack can use it to speak with our app.
  14. Rails::Application < Rails::Engine ! ! • Rails inserts the routes

    in a action_dispatch.routes key into the rack env • config.middleware.build will return a ActionDispatch::MiddlewareStack.
  15. ActionDispatch::MiddlewareStack • This middleware stack object composed of middlewares. •

    Each middleware is instantiated with the one that follows him in the stack. • The last middleware is instantiated with the actual rack app. • Invoking call on this object will go through all the middlewares and the app.
  16. Middleware stack of Rails 4.1 app

  17. run MyApp::Application.routes • ActionDispatch::Routing::RouteSet.new in railties/lib/rails/engine.rb • @router is an

    instance of Journey::Router • we are inside the Journey engine, the rails router. • Its job is to find the correct route defined in config/routes.rb for the incoming request. • The router will choose the appropriate controller and action for this request, and place in the Rack env a hash containing them.
  18. ActionDispatch::Routing::RouteSet::Dispatcher • Rails normalizes the params, and tries to get

    a controller from them. params[:controller] • If the controller is not found, Rails will return an empty response with the HTTP status code 404. • X-Cascade • dispatch(controller, params[:action], env)
  19. ActionController::Base < ActionController::Metal

  20. ActionController::Rendering

  21. We did it! • Yes ! • We followed the

    request from the beginning to the final action call.
  22. What Rails Gives us

  23. Ingredients of Rails • gem install rails • let’s look

    at rails.gemspec ! • This gem doesn’t really include any program.
  24. Rails Dependencies • It has no code, but instead it

    defines several dependencies ! ! ! • It means that the rails gem is a meta package to install these 7 gems
  25. None
  26. Rails is a full-stack MVC framework

  27. What is Railties? • It is a library providing something

    underneath the rails application • or Railroad tie
  28. Rails is a full-stack MVC framework

  29. None
  30. None
  31. None
  32. Q & A • gem install rails --pre • Thank

    You! ! ! ! ! @zoraslapen