be happy with ruby on rails - ceunsp itu

42
be happy with ruby on rails

Upload: lucas-renan

Post on 05-Dec-2014

79 views

Category:

Software


0 download

DESCRIPTION

13.10.2014

TRANSCRIPT

Page 1: Be happy with Ruby on Rails - CEUNSP Itu

be happy withruby on rails

Page 2: Be happy with Ruby on Rails - CEUNSP Itu

lucas renan

Page 3: Be happy with Ruby on Rails - CEUNSP Itu
Page 4: Be happy with Ruby on Rails - CEUNSP Itu
Page 5: Be happy with Ruby on Rails - CEUNSP Itu

gurusorocaba

Page 6: Be happy with Ruby on Rails - CEUNSP Itu

wanna be adeveloper?

Page 7: Be happy with Ruby on Rails - CEUNSP Itu
Page 8: Be happy with Ruby on Rails - CEUNSP Itu

yukihiro matsumoto

Page 9: Be happy with Ruby on Rails - CEUNSP Itu

programming languageruby

Page 10: Be happy with Ruby on Rails - CEUNSP Itu

david heinemeier hansson

Page 11: Be happy with Ruby on Rails - CEUNSP Itu

web frameworkruby on rails

Page 12: Be happy with Ruby on Rails - CEUNSP Itu

$ rails new my_app

Page 13: Be happy with Ruby on Rails - CEUNSP Itu

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’

Page 14: Be happy with Ruby on Rails - CEUNSP Itu

modularity

Page 15: Be happy with Ruby on Rails - CEUNSP Itu

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"

Page 16: Be happy with Ruby on Rails - CEUNSP Itu

environments

Page 17: Be happy with Ruby on Rails - CEUNSP Itu

config/database.yml

development: adapter: sqlite3 database: db/development.sqlite3 !test: adapter: sqlite3 database: db/test.sqlite3 !production: adapter: sqlite3 database: db/production.sqlite3

Page 18: Be happy with Ruby on Rails - CEUNSP Itu

$ rails g scaffold post title content:text

Page 19: Be happy with Ruby on Rails - CEUNSP Itu

migrations

Page 20: Be happy with Ruby on Rails - CEUNSP Itu

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

Page 21: Be happy with Ruby on Rails - CEUNSP Itu

$ rake db:migrate

Page 22: Be happy with Ruby on Rails - CEUNSP Itu

models

Page 23: Be happy with Ruby on Rails - CEUNSP Itu

app/models/post.rb

class Post < ActiveRecord::Base end

Page 24: Be happy with Ruby on Rails - CEUNSP Itu

$ rails c

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

Page 25: Be happy with Ruby on Rails - CEUNSP Itu

$ rails c

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

Page 26: Be happy with Ruby on Rails - CEUNSP Itu

routes

Page 27: Be happy with Ruby on Rails - CEUNSP Itu

$ 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

Page 28: Be happy with Ruby on Rails - CEUNSP Itu

controllers

Page 29: Be happy with Ruby on Rails - CEUNSP Itu

app/controllers/posts_controllers.rb

class PostsController < ApplicationController ! # GET /posts # GET /posts.json def index @posts = Post.all end

Page 30: Be happy with Ruby on Rails - CEUNSP Itu

views

Page 31: Be happy with Ruby on Rails - CEUNSP Itu

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

Page 32: Be happy with Ruby on Rails - CEUNSP Itu

app/views/posts/_form.html.erb

<%= form_for(@post) do |f| %> ! <%= f.label :title %> <%= f.text_field :title %> ! <%= f.submit %> !<% end %>

Page 33: Be happy with Ruby on Rails - CEUNSP Itu

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

Page 34: Be happy with Ruby on Rails - CEUNSP Itu

asset pipeline

Page 35: Be happy with Ruby on Rails - CEUNSP Itu

app/stylesheets/application.css

/* *= require_tree . *= require_self */

Page 36: Be happy with Ruby on Rails - CEUNSP Itu

app/javascripts/application.js

//= require jquery //= require jquery_ujs //= require_tree .

Page 37: Be happy with Ruby on Rails - CEUNSP Itu

app/

helpers/!mailers/!services/!uploaders/!presenters/!whatever/

Page 38: Be happy with Ruby on Rails - CEUNSP Itu

tests

Page 39: Be happy with Ruby on Rails - CEUNSP Itu

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

Page 40: Be happy with Ruby on Rails - CEUNSP Itu

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

Page 41: Be happy with Ruby on Rails - CEUNSP Itu

show me in action!

Page 42: Be happy with Ruby on Rails - CEUNSP Itu

thanks :)