Skip to content

Instantly share code, notes, and snippets.

@humblewolf
Last active December 10, 2020 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humblewolf/90446aa279455ad7aab611c743f5f72c to your computer and use it in GitHub Desktop.
Save humblewolf/90446aa279455ad7aab611c743f5f72c to your computer and use it in GitHub Desktop.
Ruby on Rails Cheatsheet
About Rails ::
1. Rails is a web framework which tries to do the heavy lifting for us (like django in py), you just need to insert code-snippets/files at pre-defined places and you are done most of the times.
2. Everything revolves around a resource, A resource is anything that can be created, updated, viewed, listed, deleted etc via http apis. For ex: For a blogging application an article can be a resource.
3. It is MVC arch : Model-View-Controller
Model:
1. Contains the blueprint/skleton of the resource (for ex. Article => Title, Body, Date-of-creation)
2. Tightly in association with DB, Typically a db table is created for every model and you can modify the record in the table by calling methods on your model object
Controller:
1. The brain of your resource.
2. Will have methods like index, new, create, show, edit, update and destroy etc. which will be invoked on calling of their respective routes (more on routes later)
View:
1. Files that help you draw on screen
2. Mostly erb(embedded ruby) files, Data objects created in controller are accessible in the respective view so that you can put the data on screen.
---------------Routes-------------------
If you write `resources :articles` in your routes file, rails will generate 8 routes for you automatically (type `rails routes` in your console while staying in the root of your rails project, for checking what routes you have in your application)
GET /atticles ---> controller#index (returns html page with all articles listed, like a dashboard)
POST /articles ---> controller#create
GET /articles/new ---> controller#new (returns html page to create new article)
GET /articles/:id ---> controller#show (returns html page showing a particular article)
GET /articles/:id/edit---> controller#edit (returns html page to edit a particular article)
PATCH /articles/:id ---> controller#update
PUT /articles/:id ---> controller#update
DELETE /articles/:id ---> controller#destroy
----------Useful links-----------
ruby :: https://learnxinyminutes.com/docs/ruby/
rails :: https://guides.rubyonrails.org/getting_started.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment