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. 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’
  2. 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"
  3. config/database.yml development: adapter: sqlite3 database: db/development.sqlite3 ! test: adapter: sqlite3

    database: db/test.sqlite3 ! production: adapter: sqlite3 database: db/production.sqlite3
  4. $ rails c post = Post.new(title: "I love ruby") post.save

    #INSERT INTO "posts" (“title”) VALUES (?) [["title", "I love ruby”]]
  5. $ rails c Post.all #SELECT "posts".* FROM "posts" ! Post.find

    1 # SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 1]]
  6. $ 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
  7. 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 %>
  8. app/views/posts/_form.html.erb <%= form_for(@post) do |f| %> ! <%= f.label :title

    %> <%= f.text_field :title %> ! <%= f.submit %> ! <% end %>
  9. 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
  10. 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
  11. 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