Rails
Rails is a web application framework written in Ruby. In comparison to something like Node's Express, it is a much higher level instance of an application framework.
From the Ruby on Rails page[1]:
The Rails philosophy includes two major guiding principles:
- Don't Repeat Yourself: DRY is a principle of software development which states that "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system". By not writing the same information over and over again, our code is more maintainable, more extensible, and less buggy.
- Convention Over Configuration: Rails has opinions about the best way to do many things in a web application, and defaults to this set of conventions, rather than require that you specify minutiae through endless configuration files.
What this means is that there is quite a bit of abstraction done right out of the gate to handle the things that are most commonly implemented in websites. If you are coming from a Javascript-oriented background, this amount of abstraction and meta-programming might feel very overwhelming, but the more time you spend learning about it, you'll see that it can be used very effectively for specific types of sites.
If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.[1]
Getting Started
Prepare
- Install
rbenv. Restart your terminal. - Install
rubyusingrbenv. Get the latest version listed usingrbenv install -land use the latest version. e.g.rbenv install 3.4.7. Restart your terminal. - Install rails using
gem install rails. Restart your terminal.
Initialization
- Run
rails new <project-name>. This initializes agitrepo and prepares all the folders and everything. - Commit all the new files.
Models (Database)
Database additions/modifications are done in a series of migrations. These are kept in the db/migrations folder and are just a record of the changes so it can be rebuilt from scratch.
- Create a new
sqlitedatabase usingbin/rails db:create. - Create a new table using
bin/rails generate model <table> <column:type>. The types can be any listed when usingbin/rails generate model --help. - Create references between tables using
bin/rails generate migration Add<TableA>RefTo<TableB> table_a_column:references.
Controllers
- Add your routes to the
config/routes.rbfile (see the getting started page[1] for more info here). - Create a new controller using
bin/rails generate controller <table> --skip-routes(use that flag only if you added routes in the before step). - Update the routes as you see fit in the
routes.rbfile. Use the shortcutresources :<table>if you want a standard set of CRUD routes[1].
View
With the new controllers added to reference the model, you can now start getting these resources on a webpage.
References
- https://guides.rubyonrails.org/getting_started.html
- https://www.railstutorial.org/
- https://www.learnrails.dev/guides/setup
Last modified: 202512120615