Ruby on Rails

Creating a new Project

1
2
rails new taski 
rails new myapp --database=postgresql

1
rails new --help
1
rake db:create:all

Starting Rails Server

1
2
3
4
rails server
or

rails s

Should you Use Scaffolds or Generators

  • Scaffolds: add pretty much useless code also.
  • Generators: We will use Generators.

Creating Your First Rails Scaffold

1
rails g scaffold Project title:string description:text percent_complete:decimal

1
rake db:migrate

Introduction to the Rails Console

1
rails c

Sandbox Mode (Any modifications you make will be rolled back on exit)

1
rails c --sandbox

How to Create Records in the Rails Console

1
2
3
10.times do |project|
Project.create!(title: "Projcect #{project}", description:"Test Description")
end

How to Update and Delete Records in the Rails Console

1
rails c

Project.all

Project.count

p = Project.last p.update!(title:"Super title")

p.delete

Advanced Database Queries in the Rails Console

Find single record by ID

1
2
3
4
5
6
Project.find(5)

a = Project.find([5,8])
a.each do |rec|
puts rec.description
end

Find Record by String

1
2
3
4
Project.where(title:"Projcect 8")
Project.where.not(title:"Projcect 8")

Project.where("LENGTH(title) > 20")

Introduction to Routes in Ruby on Rails

RESTful Routing in Rails

app/controllers/projects_controller.rb

How to Create a Custom Controller in Rails

1
rails g controller Pages contact about home

How to Create Custom Routes for Non CRUD Pages

get 'about', to:'pages#about'

file_path: app/controllers/pages_controller.rb

Defining Instance Variable to be used in views

1
2
3
def about
    @title = "Muhammad Mohsin Mahmood"
end

How to Set the Homepage for a Rails Application

root "pages#home"

How to Integrate Routing Redirects in Rails

Overview of the Master Application Layout File

How to Use View Partials <%= render 'shared/nav' %>

file name nav in share/ should be _nav.html.erb(Partial)

How to Integrate Images into a Rails Application

1
    <%= image_tag("logos/profile.png", width:"150px")%>

Note

Fat Models/Skinny Controllers Rule

Rails Models

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
rails g model task title:string description:text project:references
rake db:migrate


rails g migration add_completed_to_tasks completed:boolean

rails g migration add_stage_to_tasks stage:integer
rake db:migrate

rails g migration change_data_type_for_stage
  def change
    change_column :projects, :stage, :string
    #Ex:- change_column("admin_users", "email", :string, :limit =>25)
  end

Installing the Devise Gem for Authentication

gem 'devise'

1
bundle install
1
rails generate devise:install

rails g devise:views

Creating user Model

1
2
3
4
5
rails g devise User

rake db:migrate

rake route | grep users

Authorization

gem 'cancan' gem 'cancancan'

Adding Jquery

https://www.botreetechnologies.com/blog/introducing-jquery-in-rails-6-using-webpacker

1
2
3
4
<% fields_for :project_bugs, @bug.project_bugs do |assign_form|%>
        <%= assign_form.hidden_field :id%>
        <%= assign_form.select_fids :project_id, Project.collect {} %>
<%end%>

Populate select with collection/Database

1
<%= form.select :project, options_from_collection_for_select(Bug.all, "id", "title"), {},:multiple=> true,:validate=> true,:class=> 'form-control' %>

Databse restart

1
2
sudo service postgresql restart
rake db:reset

Error

ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column teachers.classroom_id does not exist) LINE 1: ...ers".* FROM "teachers" INNER JOIN "classrooms" ON "teachers"...

SOlution => belongs_to in join column insted of has_many

Error

NameError (uninitialized constant Student::Teachers)

Solution: make object singlular in front of : belogns_to :patient insted of patient(s)

Error

uninitialized constant Project::ProjectUser

Solution: make sure you are using the correct model name in controller like the obove error had resolved via changing ProjectUser to Project(s)User

Find current user id controller to pass in views

1
@current_user ||= User.find_by(id: session[:user_id])
Form select for collection

1
<%= form.select :project, options_from_collection_for_select(Bug.all, "id", "title"), {},:multiple=> true,:validate=> true,:class=> 'form-control' %>