Like [[Laravel]], Ruby on Rails is a [[Full Stack Framework]] for building web apps. Notably, [[GitHub]] is written in Ruby on Rails.
[[Laravel]] is based on Rails, so most things cross over.
## Basics
Rails utilizes [[Model-View-Controller]] architecture. Simply put:
* Models manage the data, such as data-tables.
* Views are the rendering responses of the application. HTML output, JSON APIs, etc.
* Controllers handle the logic for each request.
Use the `bin/rails` command to create models, controllers, views, perform migrations. It's similar to `artisan` in Laravel.
### Active Records
Active Records are the ORM-like feature of Rails. It generates the SQL for interacting with the database. They live in `app/models`
>[!tip] Use Singular Names Such as "Product" not "Products"
Active Records are based on the database columns for the table holding them. They do not define the actual columns (migrations do that). Instead, the record defines *validations*, *helpers*, and other items.
Active record code ***Does Not*** create permanent changes.
## Routing
Similar to Laravel, a route is a:
* HTTP Method
* Path (which may include variables like `:id`)
* Controller+Method
Resources create all CRUD methods (index, create, new, edit, show, update, destroy)
The root route specifies the initial route for `/`
## Controllers
Instance variables (`@nameOfVariable`) allow passing information from controllers to embedded ruby in the view.
## Views
Views are stored in `app/views/` as a set of `.html.erb` files. These are embedded ruby files which will be rendered with data from the controller.
Use the `link_to name, variable` helper to generate links to models.
## Debugging
For rendering the output of variables, use the ERB:
```ruby
<%= debug @instanceVariable %>
```
Additionally, `rails console` is similar to Laravel's `php artisan tinker`.