Date post: | 05-Dec-2014 |
Category: |
Software |
Upload: | lucas-renan |
View: | 77 times |
Download: | 0 times |
be happy withruby on rails
lucas renan
gurusorocaba
wanna be adeveloper?
yukihiro matsumoto
programming languageruby
david heinemeier hansson
web frameworkruby on rails
$ rails new my_app
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’
modularity
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"
environments
config/database.yml
development: adapter: sqlite3 database: db/development.sqlite3 !test: adapter: sqlite3 database: db/test.sqlite3 !production: adapter: sqlite3 database: db/production.sqlite3
$ rails g scaffold post title content:text
migrations
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
$ rake db:migrate
models
app/models/post.rb
class Post < ActiveRecord::Base end
$ rails c
post = Post.new(title: "I love ruby") post.save #INSERT INTO "posts" (“title”) VALUES (?) [["title", "I love ruby”]]
$ rails c
Post.all #SELECT "posts".* FROM "posts" !Post.find 1 # SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 1]]
routes
$ 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
controllers
app/controllers/posts_controllers.rb
class PostsController < ApplicationController ! # GET /posts # GET /posts.json def index @posts = Post.all end
views
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 %>
app/views/posts/_form.html.erb
<%= form_for(@post) do |f| %> ! <%= f.label :title %> <%= f.text_field :title %> ! <%= f.submit %> !<% end %>
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
asset pipeline
app/stylesheets/application.css
/* *= require_tree . *= require_self */
app/javascripts/application.js
//= require jquery //= require jquery_ujs //= require_tree .
app/
helpers/!mailers/!services/!uploaders/!presenters/!whatever/
tests
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
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
show me in action!
thanks :)