On Rails is a Web-framework built in Ruby. Built in 2004 by David Heinemeier Hansson. It follows an architecture known as ModelViewController(MVC)

Development Features

  • Autoloading: Automatically loads and reloads code changes in development, eliminating need for manual server restarts.
  • Database Interaction: ActiveRecord, Rails’ ORM, simplifies database operations and allows developers to work with Ruby objects instead of writing SQL queries directly.
  • Convention over Configuration: Rails follows naming conventions that reduce the need for explicit configuration, streamlining development.

New project: rails new proj_name Code in app directory Gemfile contains the dependencies JavaScript directory for client-side code for interactivty. Configured in Hotwire. Model names are plural by convention.

Nifty stuff

  • rails generate scaffold name will build models, views, controllers
  • rake db:migrate
  • rails server will run the app locally.
  • rails generate <option> will generate some controllers, views…etc.
  • Configure the routes in config/routes.rb

Embeded Ruby

When using Ruby in an .erb file we use the embed tags. There’s two kinds:

  1. If the snippet has no output <% %>
  2. When there’s an output <%= %>

These are erb tags that can replace HTML anchor tags. General syntax:

<%= link_to(name = nil, options = nil, html_options = nil, &block) %>

Example:

<%= link_to 'Home', root_path, class:"nav-btn"%>

This example includes the name, target, and html option for class.

CRUD in Rails

Scaffolds

These allows us to quickly build up the stuff we need to set up the CRUD infrastructure. It also builds out the DB schemas. We can generate the scaffold using the rails g command. For example: rails g scaffold rivals name:string phrase:string health:integer Then Rails will make a whole bunch of stuff including the **migration** in the dbfolder. Once this is made, the schema needs to be *pushed* to the db. To do this we simply input the following command into our terminal:rails db:migratethis will result in the creation of theschema.rb` file.