Ruby on Rails¶
Creating a new Project
1 2 | |
1 | |
1 | |
Starting Rails Server
1 2 3 4 | |
Should you Use Scaffolds or Generators
- Scaffolds: add pretty much useless code also.
- Generators: We will use Generators.
Creating Your First Rails Scaffold
1 | |
1 | |
Introduction to the Rails Console¶
1 | |
Sandbox Mode (Any modifications you make will be rolled back on exit)
1 | |
How to Create Records in the Rails Console
1 2 3 | |
How to Update and Delete Records in the Rails Console
1 | |
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 | |
Find Record by String
1 2 3 4 | |
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 | |
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 | |
How to Set the Homepage for a Rails Application
root "pages#home"
How to Integrate Routing Redirects in Rails
- get "*path", to:redirect('/error')
- get "blog", to: redirect("https://mohsinmdl.com/docs")
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 | |
Note
Fat Models/Skinny Controllers Rule
Rails Models¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Installing the Devise Gem for Authentication¶
gem 'devise'
1 | |
1 | |
rails g devise:views
Creating user Model
1 2 3 4 5 | |
Authorization¶
gem 'cancan' gem 'cancancan'
Adding Jquery¶
https://www.botreetechnologies.com/blog/introducing-jquery-in-rails-6-using-webpacker
1 2 3 4 | |
Populate select with collection/Database¶
1 | |
Databse restart¶
1 2 | |
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 | |
1 | |