Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby on Rails

Ruby on Rails

High Level Overview of Rails (designed as part of a practical introduction)

James Hughes

June 27, 2012
Tweet

More Decks by James Hughes

Other Decks in Programming

Transcript

  1. 1) MATURE WEB FRAMEWORK 2) BUILT ON TOP OF RUBY

    3) HEAVILY OPINIONATED 4) FULL STACK FRAMEWORK 5) EXTENSIBLE THROUGH GEMS
  2. ( model ) ( view ) ( controller ) <----->

    <-----> response <----- REQUEST <-----
  3. ( model ) ( view ) ( controller ) <----->

    <-----> ( ROUTER ) <- response <----- REQUEST -
  4. create lib/assets create lib/assets/.gitkeep create log create log/.gitkeep create public

    create public/404.html create public/422.html create public/500.html
  5. create public/favicon.ico create public/index.html create public/robots.txt create script create script/rails

    create test/fixtures create test/fixtures/.gitkeep create test/functional
  6. Fetching gem metadata from https://rubyge Using rake (0.9.2.2) Using i18n

    (0.6.0) Using multi_json (1.3.6) Using activesupport (3.2.6) Using builder (3.0.0) Using activemodel (3.2.6) Using erubis (2.7.0)
  7. Using journey (1.0.4) Using rack (1.4.1) Using rack-cache (1.2) Using

    rack-test (0.6.1) Using hike (1.2.1) Using tilt (1.3.3) Using sprockets (2.1.3) Using actionpack (3.2.6)
  8. Using mime-types (1.19) Using polyglot (0.3.3) Using treetop (1.4.10) Using

    mail (2.4.4) Using actionmailer (3.2.6) Using arel (3.0.2) Using tzinfo (0.3.33) Using activerecord (3.2.6)
  9. Using activeresource (3.2.6) Using bundler (1.1.4) Using coffee-script-source (1.3.3) Using

    execjs (1.4.0) Using coffee-script (2.2.0) Using rack-ssl (1.3.2) Using json (1.7.3) Using rdoc (3.12)
  10. Using thor (0.15.3) Using railties (3.2.6) Using coffee-rails (3.2.2) Using

    jquery-rails (2.0.2) Using rails (3.2.6) Using sass (3.1.19) Using sass-rails (3.2.5) Using sqlite3 (1.3.6)
  11. > rails server => Booting WEBrick => Rails 3.2.6 application

    starting in deve => Call with -d to detach => Ctrl-C to shutdown server [2012-06-26 19:06:56] INFO WEBrick 1.3.1
  12. > RAILS GENERATE SCAFFOLD ENTITY FIELD:TYPE ... FIELD:TYPE > RAILS

    GENERATE CONTROLLER > RAILS GENERATE MODEL > ...
  13. > rails generate scaffold news title:string invoke active_record create db/migrate/20120626181603_cr

    create app/models/news.rb invoke test_unit create test/unit/news_test.rb
  14. create test/fixtures/news.yml invoke resource_route route resources :news invoke scaffold_controller create

    app/controllers/news_control invoke erb create app/views/news create app/views/news/index.html.er
  15. class CreateNews < ActiveRecord::Migration def up create_table :news do |t|

    t.string :title t.string :body t.timestamps end end def down drop_table :news end end
  16. class News < ActiveRecord::Base attr_accessible :body, :title has_many :comments end

    class Comments < ActiveRecord::Base belongs_to :news end
  17. > RAKE ROUTES news_index GET /news news#index POST /news news#create

    new_news GET /news/new news#new edit_news GET /news/:id/edit news#edit news GET /news/:id news#show PUT /news/:id news#update DELETE /news/:id news#destroy
  18. class NewsController < ActionController::Base # GET /news # GET /news.json

    def index @news = News.all respond_to do |format| format.html # index.html.erb format.json { render json: @news } end end end
  19. <!DOCTYPE html> <html> <head> <title>Awesomeapp</title> <%= stylesheet_link_tag "application", :media =>

    "all" %> <%= javascript_include_tag "application" %> </head> <body> <%= yield %> </body> </html>
  20. <h1>Listing news</h1> <table> <tr> <th>Title</th> <th></th> <th></th> </tr> <% @news.each

    do |news| %> <tr> <td><%= news.title %></td> <td><%= link_to 'Show', news %></td> <td> <%= link_to 'Edit', edit_news_path(news) %> </td> </tr> <% end %> </table>