
On Day 1: I talked about the history of Ruby and Rails and how I set them up on Ubuntu. I also explained the problems I ran into and the approach I took to fix them.
In this article I am going to explain the Architecture and go into more detail about how Rails works.
Explain the MVC architecture?
MVC stands for Model, View and Controller it is designed to give developers a real clear separation of concern. It is designed to have components be really good at one thing and not bleed out into other things. Each component in MVC all have separate responsibilities.
What is the structure of a Rails App?
app folder: Main folder with nested files
bin: contains Binstubs for the Rails application. Binstubs are nothing but wrappers to run the gem executable scoped to your application. Can be used in place of bundle exec <command>. The default available Binstubs are bundle, rails, rake, setup, and spring and can be executed by bin/<executable>.
config: contains application configuration files
db: all database related files go inside this folder. The configuration schema, and migration files.
lib: is where all the application specific libraries goes. Application specific libraries are re-usable generic code executed from the application. Think of it as an application specific gem.
log: holds log files for each environment
public: common files for web applications robots.txt, HTTP errors
test: holds test file for the application
tmp: hold files like caches
vendor: JavaScript libraries and CSS files, files added here will become part of the asset pipeline automatically.
Gemfile: storage information about ruby gems
What folders are in the app folder?
- assets: hold Javascript, stylesheets, config, and images which are throughout the rails application.
- channels: web sockets
- controllers: used to control data between model and view
- helpers: Can be used to keep code DRY will explain more in a future article.
- jobs: cron jobs, will go into more detail later on
- mailers:if you need to send email, google mailers
- models: Its a blue print
- views: Takes data and displays it to browser
What is in the assets folder?
config: configuration files
images: all the images required from the application should go here. The images are available in view through helper like image_tag(“”) so that you specify the relative or absolute path for images.
JavaScripts: each controller can have its own JavaScript file, also has a pre-created application.js which is a manifest for the entire application javascript requirement. Rails uses the asset pipeline for compiling and serving up assets. This means the application.js is the file where you reference the application specific JS files, which are unified and minified before sending to the views. DONT CREATE FUNCTION IN THIS FILE.
stylesheets: use a stylesheet specific to each controller so you’ll
What is the channels folder?
It is a socket implentation to do real time update within your app, used for building connections that stay alive
What is in the controllers folder?
The purpose of a controller is to receive specific requests for the application. Routing decides which controller receives which requests. Often, there is more than one route to each controller, and different routes can be served by different actions. Each action’s purpose is to collect information to provide it to a view.
Basically a controller allows a view to access data, they will go through a controller to build a package. Each controller has certain methods that are built onto it that will often correlate with a rest verb and URL, that is often how methods get invoked.
It also has a concern folder: has modules that can be used across controllers. This is helpful to DRY up your code by implementing reusable functionality inside the directory. The naming convention is module_name.rb.
How do you create a controller?
To create a new controller, you can take advantage of the controller generator and tell it you want a controller called “You pick a name”, with an action called “index”. Rails will create several files and a route for you, if you navigate to app/controller/ and app/views you will find you newly created controller and view.
How do you define a method is a controller?
Each method starts with def keyword followed by method signature which creates a block, each block is closed by using the end keyword.
How do you define a instance variable?
By using the @ in front of a variable name, what that means is that from any instance of a class once this variable has been defined you’ll have access to it.
Started with def keyword and then method signature and that creates a block you close with end.
What is the assignment operator in Ruby?
The = sign is used as the assignment operator, right hand side will be assigned to the instance variable
What is in the helper folder?
This is where all the helper functions for view reside. There are already a few pre-created helpers available, like the one we reference above for referring to images in views. You can create your own functions in a controller specific helper file, naming convention controller_name_helper.rb.
What is in the mailers folder?
To generate a mailer rails generate mailer MailerName
When you generate a mailer, application_mailer.rb is automatically created for you. This will inherit from the ActionMailer::Base and sets the default from address as well as the layout for your mailer views. Subsequent mailers will inherit from ApplicationMailer. The naming convention is similar to controllers: modelname_mailer.rb.
What is in the models folder?
It holds a model file, A singular word in Ruby is often a model so and all Models start with a capitalized word.
Are responsible for data, logic and rules, so the model will the part of the application that interacts with your database to setup a structure validate your data and set rules around what your data needs to be look like, act like. Model is the only thing that touches your database. Rails has a built in ORM called Active Record.
What is Active Record?
Active record lives in your model, I will go into more detail in a future article about Active Record.
What is in the views folder?
view folder:It holds a view file, the view file is part of the presentation layer, includes some html and CSS, it is presented with some data that it can interact with. It has become more common these days as user interfaces have more requirements around speed and responsiveness to use something like React to use as a front-end for a rails app. In this case you would build your API in rails and use react to generate HTML in the client and rails view layer would be calls building JSON packages to hand off to the react app.
The purpose of a view is to display information in a human readable format. An important distinction to make is that it is the controller, not the view, where information is collected. View templates are written in a language called eRuby(Embedded Ruby) which is processed by the request cycle in Rails before being sent to the user.
layout folder: holds the layout for all view files
What is a Gemfile?
gem file: is where you load external packages, ruby packages are called gems and the ruby package manager is called bundler.
What is a pessimistic pin?
~> = you will give it a version number and it will only allow the last digit to update.
How do you group packages based off deployments?
You would use groups
What does the Gemfile.lock do?
Tell you what version and what packages you have and what dependenices it comes with
What is the difference between <% %> and <%= %>?
<%= %> -> means it will be rendered on the page as HTML
<% %> -> Evaulate and not render anything
What is a partial?
File name starts with _ : reusable chuck of HTML that you can embed in another view.
What are the two file extensions found in a Ruby application?
.RB — pure Ruby
.ERB — embedded Ruby -> If you make a rest call and need to display data, you can use Ruby code to display data. Parts of webpage that you would like to be dynamic.
How to set the Application Home Page?
To tell Rails where your actual home page is located you need to navigate to the config/routes.rb. The routes.rb is your application’s routing file which holds entries in a special DSL(Domain Specific Langauge) that tells Rails how to connect incoming request to controllers and actions.
What is a resource?
After creating a controller, an action and view, lets learn about a resource. Rails provides a resource method which can be used to declare a standard REST resource. You need to add the resource to the config/routes.rb, resources have CRUD operations. If you run rails routes, you’ll see that is has defined routes for all the standard RESTful actions.
What is Application Controller?
A controller is a class that is defined to inherit from Application Controller. Its inside this class that you will define methods that will become that actions for this controller. These actions will perform CRUD operations.
What are some problems with using the rails scaffolding?
makes everything plural
This article got a little longer expected, in the next article I will go over the basics of Ruby. I hope you are enjoying my articles / notes about Ruby and Rails, in the next article I will go over the basic of the Ruby syntax, starting with explaining methods, arguments, variables and much more.
Day 2: https://medium.com/@tommarler/ruby-on-rails-day-2-c51e8603c390
Day 3: https://medium.com/@tommarler/ruby-on-rails-day-3-9c6616967048
Day 3 Part 2: https://medium.com/@tommarler/ruby-on-rails-day-3-part-2-f965b0979141
Day 4: https://medium.com/@tommarler/ruby-on-rails-day-4-de85f7e71d29