Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

install rvm for ruby control version

https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-on-ubuntu-14-04-using-rvm

install rails

gem install rails -v 4.2.2

create new app

rails 4.2.2 new hello_app

rails folder structure

/app /app/assets /bin /config /db /doc /lib /lib/assets /log /public /bin/rails /test /tmp /vendor /vendor/assets /README.rdoc /Rakefile /Gemfile /Gemfile.lock /config.ru /.gitignore

Gemfile structure

source 'https://rubygems.org'

gem 'rails', '4.2.2'
# the >= notation always installs the latest gem
# the ~> only installs updated gems representing minor point releases

group :development, :test do
  gem 'spring'
end

install bundle from Gemfile

bundle install

install without production

bundle install --without production

run server

rails server -b $IP -p $PORT

for heroku deploy

gem install bundler

login heroku

heroku login

create heroku app

heroku create

deploy code

git push heroku master

visit app

heroku open

view log

heroku log --tail

generate scaffold

rails generate scaffold User name:string email:string

migrate using Rake

bundle exec rake db:migrate

rollback

bundle exec rake db:rollback

list Rake command

bundle exec rake -T db

open console

rails console

route

route structure

Rails.application.routes.draw do
  root 'application#hello'
  
  resources :users
  
  get 'static_pages/home'
end

generate controller

rails generate controller {ControllerName} {action} {action}

controller location

app/controllers/*_controller.rb

controller structure

class ApplicationController < ActionController::Base
  def hello
    render text: "hello, world!"
  end
end

model in controller

class UsersController < ApplicationController
  def idnex
    @users = User.all
    # @ called instance variable
  end
end

destory controller

rails destroy controller {ControllerName} {action} {action}

generate Model

rails generate model User name:string email:string

Model location

app/models/user.rb

Model

class User < ActiveRecord::Base

end

Model Validate

class Micropost < ActiveRecord::Base
  validates :content, length: { maximum: 140 }
end

Model has_many

claass User < ActiveRecord::Base
  has_many :microposts
end
class Micropost < ActiveRecord::Base
  belongs_to :user
end

Model presence

class Micropost < ActiveRecord::base
  belongs_to :user
  validates :content, length: { maximum: 140 },
                      presence: true
end

destroy Model

rails destroy model User

Template

app/views/users/index.html.erb

<%= %>
<%= link_to 'text', path %>
<%= provide(:title, "Home") %>
<%= yield(:title) %>
<%= render 'layouts/shim' %>

Test

test controller location

test/controllers/static_pages_controller_test.rb

test static_pages_controller_test.rb

require 'test_helper'

class StaticPagesControllerTest < ActionController::TestCase
  test "should get home" do
    get :home
    assert_response :success
    assert_select "title", "Home | Ruby on Rails Tutorial Sample App"
  end
end

test integration

rails generate integration_test site_layout
require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest
  test "layout link" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
  end
end

Model test

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  def setup
    @user = User.new(name: "Example User", email: "user@examle.com")
  end
  
  test "should be valid" do
    assert @user.valid?
  end
end

run test

bundle exec rake test

Ruby

https://www.railstutorial.org/book/rails_flavored_ruby

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.