Skip to content

Instantly share code, notes, and snippets.

@utsukushiihime
Forked from yshmarov/0 AWS Cloud9
Last active December 1, 2020 22:12
Show Gist options
  • Save utsukushiihime/0a2043ea07235c7d6156a94462d38ef3 to your computer and use it in GitHub Desktop.
Save utsukushiihime/0a2043ea07235c7d6156a94462d38ef3 to your computer and use it in GitHub Desktop.
Ruby on Rails 6: Learn 25+ gems and build a Startup MVP 2020
# All AWS C9 envments
https://eu-central-1.console.aws.amazon.com/cloud9/home?region=us-east-1
# Instance management
https://console.aws.amazon.com/ec2/home?region=eu-central-1#Instances:sort=instanceId
# Create AWS C9 environment
https://eu-central-1.console.aws.amazon.com/cloud9/home/create
Setting - set tabs to 2
Ctrl+E to search file
Alt+T - new terminal
Alt+W - close current tab
Ctrl+] - next tab
Ctrl+[ - prev tab
Ctrl+D - delete current line of code
# install latest version of Ruby, Rails, Postgresql, Yarn, Webpacker
rails -v
ruby -v
rvm list
rvm install ruby-2.7.2
rvm --default use 2.7.2
rvm uninstall 2.7.1
rvm uninstall 2.7.0
rvm uninstall 2.6.3
rvm uninstall 2.6.5
gem install rails -v 6.0.3
gem update rails
gem update --system
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install postgresql libpq-dev redis-server redis-tools yarn
ruby -v
rails -v
pg_config --version
# create rails app
rails new myappname --database=postgresql
cd myappname
bundle
yarn
# postgresql setup
sudo su postgres
createuser --interactive
ubuntu
y
exit
# to make the server work, add the url to development.rb
config.hosts << "2b8c1faf3a934c25b7e01d446161bfff.vfs.cloud9.us-east-1.amazonaws.com"
# start server
rails db:create
rails db:migrate
rails s
#Ruby on Rails
rvm install ruby-2.7.1
rvm --default use 2.7.1
rvm uninstall 2.6.5
rvm uninstall 2.6.6
rvm uninstall 2.6.3
gem install rails -v 5.2.4.3
#Postgresql
sudo apt install postgresql libpq-dev
sudo su postgres
createuser --interactive
ubuntu
y
exit
http://github.com/
git config --global user.name "Yaro"
git config --global user.email yshmarov@gmail.com
git init
git status
git add -A
git commit -m 'initialize app'
# https://github.com/new
git remote add origin https://github.com/yshmarov/rubygems.git
git push -u origin main
// delete last commit from github
git reset HEAD^ --hard
git push -f
// git uncheck last commit
git reset --soft HEAD~1
// git reset to specific commit
git reset --hard c14809fa
// forward-moving undo last commit
git revert HEAD
git add -A
git commit -m 'undo last commit'
// forward-moving undo commit-before-last-commit
git revert HEAD~1
git add -A
git commit -m 'undo commit-before-last-commit'
//check to which repo you are connected
git remote -v
//set remote repo
git remote set-url origin https://github.com/yshmarov/edurge.git
Git branches:
//Create branch
git checkout -b "i18n"
//Show all branches:
git branch
//Switch to master
git checkout master
//Merge "i18n" with master
git merge i18n
//Delete "i18n" branch
git branch -D i18n
http://heroku.com/
### If watchers error - Increasing the amount of inotify watchers
[Go Here] (https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers)
# creating the first page
rails g controller home index
# Add the following line to routes.rb
root 'home#index'
git add -A
git commit -m 'add home index controller'
# installing heroku
npm uninstall -g heroku-cli
sudo snap install heroku --classic
npm install -g heroku
# If you’d prefer to stay in the CLI to enter your credentials, you may run heroku login -i
heroku login -i
heroku: Enter your login credentials
Email: me@example.com
Password: ***************
Two-factor code: ********
Logged in as me@heroku.com
heroku create
heroku buildpacks:add heroku/ruby
git remote -v
git push heroku main
heroku run rake db:migrate
# if you want to connect to an existing heroku app
heroku git:remote -a yourappnamegoeshere
# see logs
heroku logs --tail
# to run console activerecord commands
heroku run rails c
# restart
heroku restart
# log into heroku from console
heroku login -i
https://edgeguides.rubyonrails.org/action_text_overview.html
// Console
rails action_text:install
// application.js
require("trix")
require("@rails/actiontext")
// actiontext.scss
@import "trix/dist/trix";
// application.scss
@import "./actiontext.scss";
// app/models/course.rb
class Course < ApplicationRecord
has_rich_text :description
end
// app/views/courses/_form.html.erb
<%= f.label :description %>
<%= f.rich_text_area :description %>
// application.html.haml
= render 'layouts/messages'
// _messages.html.haml
- flash.each do |name, msg|
- if msg.is_a?(String)
%div{:class => "alert alert-#{name.to_s == 'notice' ? 'success' : 'danger'}", :role => "alert"}
%button.close{"aria-hidden" => "true", "data-dismiss" => "alert", :type => "button"} ×
= content_tag :div, msg, :id => "flash_#{name}"
// courses_controller.rb
def index
if params[:title]
@courses = Course.where('title ILIKE ?', "%#{params[:title]}%") #case-insensitive
else
@courses = Course.all
end
end
// _header.html.haml
.form-inline.my-2.my-lg-0
= form_tag(courses_path, method: :get) do
.input-group
= text_field_tag :title, params[:title], autocomplete: 'off', placeholder: "Find a course", class: 'form-control-sm'
%span.input-group-append
%button.btn.btn-primary.btn-sm{:type => "submit"}
%span.fa.fa-search{"aria-hidden" => "true"}
CREATE CREDENTIALS & EDIT
rails credentials:edit
EDITOR=vim rails credentials:edit
WORKING WITH VIM
For inserting
Press i //Do required editing
For exiting
Press Esc
:wq //for exiting and saving
:q! //for exiting without saving
FIND A CREDENTIAL
rails c
Rails.application.credentials.dig(:aws, :access_key_id)
or if an env variable is used
Rails.application.credentials[Rails.env.to_sym][:aws][:access_key_id]
In production:
heroku config:set RAILS_MASTER_KEY=123456789
or
heroku config:set RAILS_MASTER_KEY=`cat config/master.key`
// courses_controller.rb
def index
if params[:name]
@courses = Course.published.approved.where('name ILIKE ?', "%#{params[:name]}%") #case-insensitive
#@courses = Course.where('name LIKE ?', "%#{params[:name]}%") #case-sensitive
#@courses = Course.where('LOWER(name) LIKE LOWER(?)', "%#{params[:name]}%") #make lowercase
else
@courses = Course.published.approved.all
end
end
// in any view:
.form-inline.my-2.my-lg-0
= form_tag(posts_path, method: :get) do
.input-group
= text_field_tag :name, params[:name], autocomplete: 'off', placeholder: "Find a post", class: 'form-control-sm'
%span.input-group-append
%button.btn.btn-primary.btn-sm{:type => "submit"}
%span.fa.fa-search{"aria-hidden" => "true"}
1. Migrations & configuration
rails g migration create_tags name course_tags_count:integer
, null: false, default: 0
rails g migration create_course_tags course:references tag:references
course.rb
has_many :course_tags, inverse_of: :course, dependent: :destroy
has_many :tags, through: :course_tags
tag.rb
class Tag < ApplicationRecord
has_many :course_tags
has_many :courses, through: :course_tags
validates :name, length: {minimum: 1, maximum: 25}, uniqueness: true
def to_s
name
end
def popular_name
"#{name.to_s}: #{course_tags_count.to_s}"
end
course_tag.rb
#Tag.find_each { |tag| Tag.reset_counters(tag.id, :course_tags) }
belongs_to :tag, counter_cache: true
belongs_to :course
routes.rb
resources :tags
courses_controller.rb
, course_tag_ids: []
_course.html.haml
= link_to tag.name, tag_path(tag), class: 'text-white'
/= link_to tag.name, courses_path(courses_search: {s: 'enrollments_count desc'})
rails c
Tag.create(name: "Programming")
Tag.create(name: "English")
Tag.create(name: "Literature")
2. Select tags with selectize js
yarn add selectize
app/assets/stylesheets/application.scss:
@import "selectize/dist/css/selectize";
@import "selectize/dist/css/selectize.default";
javascript/packs/application.js
require("selectize")
require("packs/tags")
javascript/packs/tags.js
$(document).on("turbolinks:load", function() {
if ($('.selectize')){
$('.selectize').selectize({
sortField: 'text'
});
}
$(".selectize-tags").selectize({
create: function(input, callback) {
$.post('/tags.json', { tag: { name: input } })
.done(function(response){
console.log(response)
callback({value: response.id, text: response.name });
})
}
});
});
course/form.html.haml
= f.select :tag_ids, Tag.all.pluck(:name, :id), {}, { multiple: true, class: "selectize-tags" }
// gemfile:
gem 'wicked_pdf' #PDF for Ruby on Rails
gem 'wkhtmltopdf-binary', group: :development
gem 'wkhtmltopdf-heroku', group: :production
config/wicked_pdf.rb
WickedPdf.config ||= {}
WickedPdf.config.merge!({
#YOUR CONFIG HERE
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment