Skip to content

costinEEST/learn-enough-rails

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rvm get stable
rvm install 3.1.2
rvm --default use 3.1.2
echo "gem: --no-document" >> .gemrc
gem install rails -v 7.0.3
gem install bundler -v 2.3.15
source <(curl -sL https://cdn.learnenough.com/resize)
rails _7.0.3_ new hello_app --skip-bundle
  • As of Rails 6 and continuing in Rails 7, running rails new automatically initializes a Git repository:
rm -r hello_app/.git
  • This would install the latest version of the capybara gem (which is used in testing) as long as it’s greater than or equal to version 3.26—even if it’s, say, version 7.2.
gem "capybara", ">= 3.26"
  • This installs the gem sqlite3 as long as it’s version 1.4 or newer (a “minor update”) but not 2 or newer (a “major update”). In other words, the >= notation always installs the latest gem as long as it meets the minimum version requirement, whereas the ~> 1.4 notation will install 1.5 (if available) but not 2.0.
gem "sqlite3", "~> 1.4"
cd hello_app/ && bundle install
bin/rails server
ls app/controllers/*_controller.rb
  • Example of how to define the route root:
root "controller_name#action_name"
  • Update Git on Cloud9:
source <(curl -sL https://cdn.learnenough.com/upgrade_git)
git config --global --list

credential.helper=!aws codecommit credential-helper $@
credential.usehttppath=true
core.editor=nano
  • Configure the name and email fields for Git:
$ git config --global user.name "Your Name"
$ git config --global user.email your.email@example.com
  • Define the default branch's name:
git config --global init.defaultBranch main
  • Set up git co as a checkout alias:
git config --global alias.co checkout
  • Configure Git to remember passwords for a set length of time:
git config --global credential.helper "cache --timeout=86400"
  • Prevent the local installation of any production gems (which in this case consists of the pg gem):
bundle config
  • Bundling without production gems:
bundle _2.3.14_ config set --local without 'production'
bundle _2.3.14_ install
bundle _2.3.14_ lock --add-platform x86_64-linux
  • See if your system already has the Heroku command-line client installed:
heroku --version
  • Install Heroku on the cloud IDE:
source <(curl -sL https://cdn.learnenough.com/heroku_install)
heroku login
  • On the cloud IDE:
heroku login --interactive

Email: <your email>
Password: <your API Key, NOT your Heroku password>
  • Create and configure a new application at Heroku:
heroku create
  • Push the main branch up to Heroku:
git push heroku main
  • If something goes wrong, inspect the logs:
heroku logs
  • Get info about the Heroku app, including the Web URL:
heroku apps:info
('a'..'z').to_a.shuffle[0..7].join
  • Rename the application
heroku rename learn-enough-rails_hello-app
rails new toy_app --skip-bundle
cd toy_app
bundle config set --local without 'production'
bundle install
toy_app/bin/rails s
git commit -am "Add hello"
heroku create
git push && git push heroku main
heroku rename learn-enough-rails-toy-app
rails generate scaffold User name:string email:string
rails db:migrate
heroku run rails db:migrate
  • The correspondence between pages and URLs for the Users resource:
URL Action Purpose
/users index page to list all users
/users/1 show page to show user with id 1
/users/new new page to make a new user
/users/1/edit edit page to edit user with id 1
  • RESTful routes provided by the Users resource:
HTTP request method URL Action Purpose
GET /users index page to list all users
GET /users/1 show page to show user with id 1
GET /users/new new page to make a new user
POST /users create create a new user
GET /users/1/edit edit page to edit user with id 1
PATCH /users/1 update update user with id 1
DELETE /users/1 delete delete user with id 1
rails generate scaffold Micropost content:text user_id:integer
rails db:migrate
  • Undo a single migration step:
rails db:rollback
  • Go all the way back to the beginning:
rails db:migrate VERSION=0
  • Try to insert a text longer than 140 characters into the Content textarea at http://127.0.0.1:3000/microposts/new then submit the form.
cd toy_app && rails console
User.first
User.create!(name: 'Cos', email: 'mail@costi.com')
micropost = User.first.microposts.first
  • The inheritance hierarchy for the User and Micropost models:

The inheritance hierarchy for the User and Micropost models

  • The inheritance hierarchy for the Users and Microposts controllers:

The inheritance hierarchy for the Users and Microposts controllers

rails new sample_app --skip-bundle
rm -rf sample_app/.git
curl --location --remote-header-name https://raw.githubusercontent.com/learnenough/rails_tutorial_7th_edition_gemfiles/master/sample_app/Gemfile_initial --output sample_app/Gemfile
  • Skip the pg gem for PostgreSQL in development and use SQLite for development and testing:
bundle config set --local without 'production'
bundle install
rails generate controller StaticPages home help
  • The inheritance hierarchy for the Static Pages:

The inheritance hierarchy for the Static Pages.

  • Undo the created controller:
rails destroy controller StaticPages home help
  • Rails shortcuts:
Full command Shortcut
rails server rails s
rails console rails c
rails generate rails g
rails test rails t
bundle install bundle
mv app/views/layouts/application.html.erb layout_file
mv layout_file app/views/layouts/application.html.erb
bundle exec guard init
curl --location --remote-header-name https://raw.githubusercontent.com/learnenough/rails_tutorial_sample_app_7th_ed/main/Guardfile --output Guardfile
bundle exec guard
nano ~/.irbrc
cat ~/.irbrc

IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:AUTO_INDENT_MODE] = false
rails console
curl -o app/assets/images/rails.svg -L https://cdn.learnenough.com/rails.svg
curl -OL https://cdn.learnenough.com/kitten.jpg
touch app/assets/stylesheets/custom.scss
  • https://github.com/rails/sprockets/blob/main/guides/how_sprockets_works.md

  • Inside of sample_app\app\assets\stylesheets\application.css the line *= require_tree . ensures that all CSS files in the app/assets/stylesheets directory (including the tree subdirectories) are included into the application.css. The line *= require_self specifies where in the loading sequence the CSS in application.css itself gets included.

  • Route and URL mapping for site links:

Page URL Named route
Home / root_path
About /about about_path
Help /help help_path
Contact /contact contact_path
Sign up /signup signup_path
Log in /login login_path
  • Generate a template test, called 'site_layout':
rails generate integration_test site_layout
rails test:integration
rails generate controller Users new
rails generate model User name:string email:string
rails db:migrate
rails console --sandbox
user = User.new
user.valid?
  • Save the User object to the database:
user.save
  • Find User by id:
User.find(1)
  • Active Record allows to find users by specific attributes:
User.find_by(email: 'michael@example.com')
User.first
User.first
user.email = "mhartl@example.net"
user.email = "mhartl@example.net"
user.save
user.reload.email
user.update(name: "The Dude", email: "dude@abides.org")
rails test:models
rails console --sandbox
user = User.new(name: "", email: "michael@example.com")

user.valid?

user.errors.full_messages
rails generate migration add_index_to_users_email
rails db:migrate
rails generate migration add_password_digest_to_users password_digest:string
bundle install
  • Creating and authenticating a user:
rails console
User.create(name: "Cos tin", email: "costin@ymail.com", password: "testingPwd", password_confirmation: "testingPwd")
User.find_by(email: 'costin@ymail.com').password_digest
User.find_by(email: 'costin@ymail.com').authenticate('wrongPwd')
User.find_by(email: 'costin@ymail.com').authenticate('testingPwd')
!!User.find_by(email: 'costin@ymail.com').authenticate('testingPwd')
  • Rails comes equipped with three environments: test, development, and production. The default environment for the Rails console is development.

  • To run a console in a different environment (to debug a test, for example), you can pass the environment as an option to the console script:

rails console --environment test
  • If you have deployed your sample app to Heroku, you can see its environment using heroku run rails console.
puts User.find_by(email: 'costin@ymail.com').attributes.to_yaml

or

y User.find_by(email: 'costin@ymail.com').attributes
  • A Users resource:
User.count
User.first
(rdbg) @user.name
"Cos tin"
(rdbg) @user.email
"costin@ymail.com"
(rdbg)
rails console
user = User.first

user.update(name: 'Constantin', email: "constantin@gmail.com", password: "parolaDemo", password_confirmation: 'parolaDemo')
user = User.new(name: "The coder", email: "mail@void", password: "ruby", password_confirmation: "ruby")

user.save

user.errors.full_messages
rails generate integration_test users_signup
rails db:migrate:reset
rails test
git add -A
git commit -m "Use SSL and the Puma web server in production"
git push && git push heroku
heroku run rails db:migrate
  • How to reset the production database:
heroku pg:reset DATABASE
heroku run rails db:migrate
rails generate controller Sessions new
rails routes -c users
   Prefix Verb   URI Pattern               Controller#Action
   signup GET    /signup(.:format)         users#new
    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
          PATCH  /users/:id(.:format)      users#update
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy
rails routes -c sessions
      Prefix Verb   URI Pattern             Controller#Action
sessions_new GET    /sessions/new(.:format) sessions#new
       login GET    /login(.:format)        sessions#new
             POST   /login(.:format)        sessions#create
      logout DELETE /logout(.:format)       sessions#destroy
curl -I http://127.0.0.1:3000 | grep Set-Cookie
rails generate integration_test users_login