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

Be happy with Ruby on Rails - CEUNSP Itu

Be happy with Ruby on Rails - CEUNSP Itu

lucas renan

October 13, 2014
Tweet

More Decks by lucas renan

Other Decks in Programming

Transcript

  1. be happy with
    ruby on rails

    View full-size slide

  2. guru
    sorocaba

    View full-size slide

  3. wanna be a
    developer?

    View full-size slide

  4. yukihiro
    matsumoto

    View full-size slide

  5. programming language
    ruby

    View full-size slide

  6. david
    heinemeier
    hansson

    View full-size slide

  7. web framework
    ruby on rails

    View full-size slide

  8. $ rails new my_app

    View full-size slide

  9. Gemfile
    source 'https://rubygems.org'
    !
    # Bundle edge Rails instead: gem 'rails', github:
    'rails/rails'
    gem 'rails', '4.1.6'
    # Use sqlite3 as the database for Active Record
    gem 'sqlite3'
    # Use SCSS for stylesheets
    gem 'sass-rails', '~> 4.0.3'
    # Use Uglifier as compressor for JavaScript assets
    gem 'uglifier', '>= 1.3.0'
    # Use CoffeeScript for .js.coffee assets and views
    gem 'coffee-rails', '~> 4.0.0’

    View full-size slide

  10. config/application.rb
    # Pick the frameworks you want:
    !
    require "active_model/railtie"
    require "active_record/railtie"
    require "action_controller/railtie"
    # require "action_mailer/railtie"
    require "action_view/railtie"
    require "sprockets/railtie"
    require "rails/test_unit/railtie"

    View full-size slide

  11. environments

    View full-size slide

  12. config/database.yml
    development:
    adapter: sqlite3
    database: db/development.sqlite3
    !
    test:
    adapter: sqlite3
    database: db/test.sqlite3
    !
    production:
    adapter: sqlite3
    database: db/production.sqlite3

    View full-size slide

  13. $ rails g scaffold post title content:text

    View full-size slide

  14. db/migrate/20141013174127_create_posts.rb
    class CreatePosts < ActiveRecord::Migration
    def change
    create_table :posts do |t|
    t.string :title
    t.text :content
    !
    t.timestamps
    end
    end
    end

    View full-size slide

  15. $ rake db:migrate

    View full-size slide

  16. app/models/post.rb
    class Post < ActiveRecord::Base
    end

    View full-size slide

  17. $ rails c
    post = Post.new(title: "I love ruby")
    post.save
    #INSERT INTO "posts" (“title”)
    VALUES (?) [["title", "I love ruby”]]

    View full-size slide

  18. $ rails c
    Post.all
    #SELECT "posts".* FROM "posts"
    !
    Post.find 1
    # SELECT "posts".* FROM "posts" WHERE
    "posts"."id" = ? LIMIT 1 [["id", 1]]

    View full-size slide

  19. $ rake routes
    Prefix Verb URI Pattern Controller#Action
    !
    posts GET /posts(.:format) posts#index
    POST /posts(.:format) posts#create
    new_post GET /posts/new(.:format) posts#new
    edit_post GET /posts/:id/edit(.:format) posts#edit
    post GET /posts/:id(.:format) posts#show
    PATCH /posts/:id(.:format) posts#update
    PUT /posts/:id(.:format) posts#update
    DELETE /posts/:id(.:format) posts#destroy

    View full-size slide

  20. app/controllers/posts_controllers.rb
    class PostsController < ApplicationController
    !
    # GET /posts
    # GET /posts.json
    def index
    @posts = Post.all
    end

    View full-size slide

  21. app/views/posts/index.html.erb
    <% @posts.each do |post| %>
    !
    <%= post.title %>
    !
    <%= link_to 'Show', post %>
    <%= link_to 'Edit', edit_post_path(post) %>
    <%= link_to 'Destroy', post, method: :delete,
    data: { confirm: 'Are you sure?' } %>
    !
    <% end %>

    View full-size slide

  22. app/views/posts/_form.html.erb
    <%= form_for(@post) do |f| %>
    !
    <%= f.label :title %>
    <%= f.text_field :title %>
    !
    <%= f.submit %>
    !
    <% end %>

    View full-size slide

  23. app/controllers/posts_controllers.rb
    class PostsController < ApplicationController
    !
    # POST /posts
    def create
    @post = Post.new(post_params)
    !
    respond_to do |format|
    if @post.save
    format.html { redirect_to @post, notice: 'Post
    was successfully created.' }
    else
    format.html { render :new }
    end
    end
    end

    View full-size slide

  24. asset pipeline

    View full-size slide

  25. app/stylesheets/application.css
    /*
    *= require_tree .
    *= require_self
    */

    View full-size slide

  26. app/javascripts/application.js
    //= require jquery
    //= require jquery_ujs
    //= require_tree .

    View full-size slide

  27. app/
    helpers/!
    mailers/!
    services/!
    uploaders/!
    presenters/!
    whatever/

    View full-size slide

  28. test/controllers/posts_controller_test.rb
    class PostsControllerTest < ActionController::TestCase
    setup do
    @post = posts(:one)
    end
    !
    test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:posts)
    end

    View full-size slide

  29. test/controllers/posts_controller_test.rb
    class PostsControllerTest < ActionController::TestCase
    setup do
    @post = posts(:one)
    end
    !
    test "should create post" do
    assert_difference('Post.count') do
    post :create, post: { title: @post.title }
    end
    !
    assert_redirected_to post_path(assigns(:post))
    end

    View full-size slide

  30. show me in action!

    View full-size slide