- https://aws.amazon.com/cloud9
- https://eu-south-1.console.aws.amazon.com/cloud9/home/product?ad=c&cp=bn&p=c9®ion=eu-south-1#
- https://eu-south-1.console.aws.amazon.com/cloud9/ide/cf7e24b3df894167a884d128c4695a65
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 inRails 7
, runningrails 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 version3.26
—even if it’s, say, version7.2
.
gem "capybara", ">= 3.26"
- This installs the gem
sqlite3
as long as it’s version1.4
or newer (a “minor update”) but not2
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 install1.5
(if available) but not2.0
.
gem "sqlite3", "~> 1.4"
cd hello_app/ && bundle install
bin/rails server
-
To view the result of
rails server
on a native OS, paste the URL http://localhost:3000 into the address bar of your browser. On the cloud IDE, go toPreview
and click onPreview Running Application
and then open it in a full browser window or tab (https://cf7e24b3df894167a884d128c4695a65.vfs.cloud9.eu-south-1.amazonaws.com). -
Look for all controllers available
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)
-
https://devcenter.heroku.com/articles/heroku-cli#install-the-heroku-cli
-
On a native system:
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
-
Generate a random subdomain with Ruby:
('a'..'z').to_a.shuffle[0..7].join
- Rename the application
heroku rename learn-enough-rails_hello-app
- https://learn-enough-rails-hello-app.herokuapp.com
- David Heinemeier Hansson using scaffolding in Rails
rails new toy_app --skip-bundle
cd toy_app
bundle config set --local without 'production'
bundle install
- https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig + https://stackoverflow.com/a/54696441/1904223 + https://youtu.be/_fcQDS1iTPw
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
- https://learn-enough-rails-toy-app.herokuapp.com + https://dashboard.heroku.com/apps/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 athttp://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 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
- https://github.com/learnenough/rails_tutorial_sample_app_7th_ed/blob/main/README.md
- Easily include static pages in your Rails app
rails generate controller StaticPages home help
- 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 |
- http://127.0.0.1:3000/static_pages/home
- http://127.0.0.1:3000/static_pages/help
- https://dhh.dk/2014/tdd-is-dead-long-live-testing.html
mv app/views/layouts/application.html.erb layout_file
mv layout_file app/views/layouts/application.html.erb
- https://github.com/kern/minitest-reporters
- https://github.com/guard/guard
- https://www.railstutorial.org/guardfile
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
- https://www.learnenough.com/ruby
- https://en.wikipedia.org/wiki/Media_type
- https://en.wikipedia.org/wiki/Abstraction_layer
- stylesheet_link_tag(*sources)
- https://www.learnenough.com/text-editor
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 theapp/assets/stylesheets
directory (including the tree subdirectories) are included into theapplication.css
. The line*= require_self
specifies where in the loading sequence the CSS inapplication.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
-
Explore the data models is the Rails console:
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
- https://en.wikipedia.org/wiki/Hash_function
- https://en.wikipedia.org/wiki/Cryptographic_hash_function
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
, andproduction
. The default environment for the Rails console isdevelopment
. -
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 + http://atdot.net/~ko1/activities/2021_rubyconf.pdf + https://edgeguides.rubyonrails.org/debugging_rails_applications.html#entering-a-debugging-session
(rdbg) @user.name
"Cos tin"
(rdbg) @user.email
"costin@ymail.com"
(rdbg)
- Gravatar URLs are based on an MD5 hash of the user's email address
- https://rubyapi.org/3.1/o/digest/md5
- https://rubyapi.org/3.1/o/string#method-i-downcase
- https://rubyapi.org/3.1/o/string#method-i-strip
- https://api.rubyonrails.org/classes/String.html#method-i-squish
rails console
user = User.first
user.update(name: 'Constantin', email: "constantin@gmail.com", password: "parolaDemo", password_confirmation: 'parolaDemo')
- https://thoughtbot.com/upcase/videos/ruby-keyword-arguments
- https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with
- Soft-deprecated:
- https://stackoverflow.com/questions/941594/understanding-the-rails-authenticity-token
- http://www.railsstatuscodes.com/unprocessable_entity.html
- https://guides.rubyonrails.org/layouts_and_rendering.html#the-status-option
- https://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-require
- https://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-permit
- http://127.0.0.1:3000/signup?admin=1
user = User.new(name: "The coder", email: "mail@void", password: "ruby", password_confirmation: "ruby")
user.save
user.errors.full_messages
- https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-any-3F
- https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-empty-3F
- https://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-pluralize
rails generate integration_test users_signup
-
https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to
-
https://api.rubyonrails.org/classes/ActionDispatch/Flash.html
-
Reset the database:
rails db:migrate:reset
-
https://api.rubyonrails.org/classes/ActiveSupport/Testing/Assertions.html#method-i-assert_difference
-
https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag
-
SSL (Secure Sockets Layer) is now TLS (Transport Layer Security)
-
https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server
-
https://devcenter.heroku.com/articles/getting-started-with-rails6
-
Production deployment:
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
-
Generate the Sessions controller:
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
- https://guides.rubyonrails.org/security.html#session-storage
- https://guides.rubyonrails.org/security.html#replay-attacks-for-cookiestore-sessions
- https://guides.rubyonrails.org/action_controller_overview.html#session
- https://github.com/rails/activerecord-session_store
- https://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
rails generate integration_test users_login