General Ruby on Rails concepts Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How the web works:

What is HTTP and how does it work?

A

The web has a bunch of layers (Application, TCP, Internet, Hardware layers) that are all connected. But basically, it works through HTTP (Hypertext Transfer Protocol).

The HTTP works like a request — response cycle in the client — server model.

We have a client, a web browser. We type the www.google.com URL, and the client submits the HTTP request (request message) to the server. The server returns the HTTP response (response message — in this case, the response is the HTML from Google’s website).

The client does the request and receives the response from the server. The client handles the UI and user interactions. On the server, we can store and retrieve data (on databases), process logic on the background (workers/jobs), and a lot of other things.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the MVC architecture?

A

MVC stands for Model, View, and Controller. It’s basically an architecture for how applications are built.

In this architecture, we have the “separation of the concerns” among Models, Views and, Controllers. Each part has its own responsibility.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In MVC, what is the Model and what is its role?

A

It “maintains the relationship between Object and Database and handles validation, association, transactions”

The Model represents the data, and does nothing else. Basically, the Model is a class that maps to the data table. It can have methods for manipulating the corresponding data.

Put differently:
Each model (can) represent a database table (in case of SQL databases). This model object gains capabilities (inherited from ActiveRecord — Rails class) to retrieve, save, edit, and delete data from database table. ActiveRecord allows you to present the data from database rows as objects and embellish these data objects with business logic methods.

NOTE: We use model objects as a layer between our application and the database.

Besides that relation with the database, the model can create validations and associations between models.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In MVC, what is the View and what is its role?

A

“A presentation of data in a particular format, triggered by a controller’s decision to present the data.”

This is the presentation of the request’s response. This presentation can be in a bunch of format types: PDF, HTML, JSON, etc. The final result of a view will likely be the user interface (UI) — Part of the “Client.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In MVC, what is the Controller and what is its role?

A

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services.

It is responsible for routing external requests to internal actions. It handles people-friendly URLs extremely well.

It manages helper modules, which extend the capabilities of the view templates without bulking up their code.

IN SUMMARY, the user does stuff (request to the server), the the Rails application has the router to map the URL path to the right controller. In the controller, we can do all things with a model (or more than one model) — CRUD meaning creating, reading, updating, deleting data — and render a view to the user.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the Devise gem do?

A

Devise is a popular authentication solution for Rails applications. It helps you check users credentials and let them into the site or not, or you can pick and choose additional authentication features such as:

  • password reset via email
  • locking users out after a number of failed attempts
  • email activation being required before a new account is allowed to sign in
  • timing users out after a period of inactivity

Basically, it’s a Rails tool/solution for managing user authentication.

Footnote: Devise takes care of a lot of the legwork of setting up an authentication system. You could build your own with bcrypt or the Active record secure password functionality but you’d need to implement the forms, helpers and emails on your own. Devise is a 10 minute set up.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are params? What are the 3 types of params?

A

Params are key:value pairs (hashes) passed from the users browsers to the application.

Three groups of parameters:

  1. User supplied parameters (query string)
    - GET (http://domain.com/url?param1=value1&param2=value2 will set params[:param1] and params[:param2])
    - POST (e.g. JSON, XML will automatically be parsed and stored in params)
    - Note: By default, Rails duplicates the user supplied parameters and stores them in params[:user] if in UsersController, can be changed with wrap_parameters setting
  2. Routing parameters – links to a specific object (uses the id of the object, like a SKU)
    match ‘/user/:id’ in routes.rb will set params[:id]
  3. Post params – info to pass in arguments in creating e.g. articles, products, blog posts, user profile, etc., through a form.
  4. Default parameters
    params[:controller] and params[:action] is always available and contains the current controller and action

Rails uses something routes to direct requests to their corresponding controller actions. These routes may contain segments that are extracted from the URL and put into params for that controller. For example, a URL like http://example.com/products/42 will set params[:id] to 42.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Rails?

A

An open source Ruby framework for developing database-backed web applications.

Full Stack Framework
- includes everything needed to create a database-driven web application, using the Model-View-Controller pattern.

Convention over Configuration
- Rails shuns configuration files in favor of conventions, reflection, and dynamic runtime extensions. (Most web development frameworks for .NET or Java force you to write pages of configuration code.)

Your application code and your running database already contain everything that Rails needs to know!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Rails strengths:

  • What is Active Record framework?
A

Rails introduces the Active Record framework, which saves objects into the database. The Rails version of the Active Record discovers the columns in a database schema and automatically attaches them to your domain objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Rails strengths:

  • What is Scaffolding?
A

Scaffolding generally refers to a quickly set up skeleton for an app (other platforms have it as well). Rails basically creates basic CRUD (create, read, update, delete) screens.

You then go through and add the code to manage the data the way you want replacing the scaffolding. It is generally only intended to get you up and running quickly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Rails strengths:

  • What is Rake?
A

The rake utility allows you to execute all your automated tests with one command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Rails strengths:

  • How many default environments does Rails create at set up and what are they for?
A

Rails gives you three default environments: development, testing, and production.

Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In order develop a web application using Rails, what four software does one need to set up?

A

To develop a web application using Ruby on Rails Framework, you need to install the following software −

Ruby
The Rails Framework
A Web Server
A Database System (e.g. PostgreSQL)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a Framework?

A

A framework is a program, set of programs, and/or code library that writes most of your application for you. When you use a framework, your job is to write the parts of the application that make it do the specific things you want.

So basically a framework is something that takes care of redundant basic tasks so that sophisticated applications can be written on top of it in simple manner. Frameworks are generally generic add-on on top of a programming language. They don’t function by themselves, code needs to be written that utilizes that framework’s feature to give the framework life.

In simple terms, you can call libraries, while frameworks call YOUR code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the three primary tasks you have to perform when you write a Rails application? (think M-V-C)

A

When you set out to write a Rails application, leaving aside the configuration and other housekeeping chores, you have to perform three primary tasks:

1) MODEL: describe and model your application’s domain
− The domain is the universe of your application. The domain may be a music store, a university, a dating service, an address book, or a hardware inventory. So here you have to figure out what’s in it, what entities exist in this universe and how the items in it relate to each other. This is equivalent to modeling a database structure to keep the entities and their relationship.

2) CONTROLLER: specify what can happen in this domain − the domain model is static; you have to make it dynamic. Addresses can be added to an address book. Musical scores can be purchased from music stores. Users can log in to a dating service. Students can register for classes at a university. You need to identify all the possible scenarios or actions that the elements of your domain can participate in.
3) VIEW: choose and design the publicly available views of the domain − At this point, you can start thinking in Web-browser terms. Once you’ve decided that your domain has students, and that they can register for classes, you can envision a welcome page, a registration page, and a confirmation page, etc. Each of these pages, or views, shows the user how things stand at a certain point.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Ruby on Rails MVC Framework – the Model View Controller principle divides the work of an application into three separate but closely cooperative subsystems. Explain each:

Model (ActiveRecord )

View ( ActionView )

Controller ( ActionController )

A

Model (ActiveRecord)
It maintains the relationship between the objects and the database and handles validation, association, transactions, and more.

This subsystem is implemented in ActiveRecord library, which provides an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records. Ruby method names are automatically generated from the field names of database tables.

View ( ActionView )
It is a presentation of data in a particular format, triggered by a controller’s decision to present the data.

This subsystem is implemented in ActionView library, which is an Embedded Ruby (ERb) based system for defining presentation templates for data presentation in a human readable format.

Controller ( ActionController )
A controller’s purpose is to receive specific requests for the application. It’s the facility within the application that directs traffic, on the one hand, querying the models for specific data, and on the other hand, organizing that data (searching, sorting, messaging it) into a form that fits the needs of a given view.

This subsystem is implemented in ActionController, which is a data broker sitting between ActiveRecord (the database interface) and ActionView (the presentation engine).

NOTE: ActionController and ActionView are bundled together under ActionPack. These two layers are bundled in a single package due to their heavy interdependence.

ActiveRecord provides a range of programming techniques and shortcuts for manipulating data from an SQL database. ActionController and ActionView provides facilities for manipulating and displaying that data. Rails ties it all together.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

When first setting up a Rails project, what does the ‘rails demo’ command do?

A

‘rails demo’ is a Rails helper command that sets up a demo application along with the entire (default) directory structure for the application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Explain the purpose of each directory in the directory structure:

app

app/controllers

app/helpers

app/models

app/view

app/view/layouts

components

config

db

doc

lib

log

public

script

test

vendor

Apart from these directories, the following file is also in the directory:

Rakefile

A

app − It organizes your application components. It’s got subdirectories that hold the view (views and helpers), controller (controllers), and the backend business logic (models).

app/controllers − The controllers subdirectory is where Rails looks to find the controller classes. A controller handles a web request from the user.

app/helpers − The helpers subdirectory holds any helper classes used to assist the model, view, and controller classes. This helps to keep the model, view, and controller code small, focused, and uncluttered.

app/models − The models subdirectory holds the classes that model and wrap the data stored in our application’s database.

app/view − The views subdirectory holds the display templates to fill in with data from our application, convert to HTML, and return to the user’s browser.

app/view/layouts − Holds the template files for layouts to be used with views.

components − This directory holds components, tiny self-contained applications that bundle model, view, and controller.

config − This directory contains the small amount of configuration code that your application will need, including your database configuration (in database.yml), your Rails environment structure (environment.rb), and routing of incoming web requests (routes.rb). You can also tailor the behavior of the three Rails environments for test, development, and deployment with files found in the environments directory.

db − Usually, your Rails application will have model objects that access relational database tables. You can manage the relational database with scripts you create and place in this directory.

doc − Ruby has a framework, called RubyDoc, that can automatically generate documentation for code you create. You can assist RubyDoc with comments in your code. This directory holds all the RubyDoc-generated Rails and application documentation.

lib − You’ll put libraries here, unless they explicitly belong elsewhere (such as vendor libraries).

log − Error logs go here. Rails creates scripts that help you manage various error logs. You’ll find separate logs for the server (server.log) and each Rails environment (development.log, test.log, and production.log).

public − Like the public directory for a web server, this directory has web files that don’t change, such as JavaScript files (public/javascripts), graphics (public/images), stylesheets (public/stylesheets), and HTML files (public).

script − This directory holds scripts to launch and manage the various tools that you’ll use with Rails. For example, there are scripts to generate code (generate) and launch the web server (server).

test − The tests you write and those that Rails creates for you, all goes here. You’ll see a subdirectory for mocks (mocks), unit tests (unit), fixtures (fixtures), and functional tests (functional).

vendor − Libraries provided by third-party vendors (such as security libraries or database utilities beyond the basic Rails distribution) go here.

Apart from these directories, there will be two files available in demo directory.

README − This file contains a basic detail about Rail Application and description of the directory structure explained above.

Rakefile − This file helps with building, packaging and testing the Rails code. This will be used by rake utility supplied along with the Ruby installation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How does one create a new Rails application?

A

Create a project folder, mkdir into in, then run the command ‘rails new [name_for_your_project]. create a skeleton for library application. This will create the Rails directory structure in the current directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does the ‘Rails server’ command do?

A

It starts the built-in WEBrick web server. It will be started from the application directory and run on port number 3000.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How many databases does Rails normally set up and and where can you configure them (to set usernames etc)?

A

Ruby on Rails recommends to create three databases - a database each for development, testing, and production environment. According to convention, their names should be −

[project_name]_development

[project_name]_production

[project_name]_test

You should initialize all three of them and create a user and password for them with full read and write privileges. You do this in the file database.yml, available in the library\config subdirectory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

About the Model layer, what does this statement mean?

“Rails Active Record is the Object/Relational Mapping (ORM) layer supplied with Rails. It closely follows the standard ORM model.”

A

Rails Active Record is the Object/Relational Mapping (ORM) layer supplied with Rails. It closely follows the standard ORM model, which is as follows −

  • tables map to classes,
  • rows map to objects and
  • columns map to object attributes.

Rails Active Records provide an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records. Ruby method names are automatically generated from the field names of database tables.

Each Active Record object has CRUD (Create, Read, Update, and Delete) methods for database access. This strategy allows simple designs and straight forward mappings between database tables and application objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Model layer:

Translating a domain model into SQL is generally straight forward, as long as you remember that you have to write Rails-friendly SQL. In practical terms, you have to follow… what rules?

A
  • Each entity (such as book) gets a table in the database named after it, but in the plural (books).
  • Each such entity-matching table has a field called id, which contains a unique integer for each record inserted into the table.
  • Given entity x and entity y, if entity y belongs to entity x, then table y has a field called x_id.
  • The bulk of the fields (cells?) in any table store the values for that entity’s simple properties (anything that’s a number or a string).
24
Q

When setting up the project, what does running the command “rails generate model [class_name]” in the to-level directory do?

A

It create the Active Record (Model) files for the entities/classes in your application.

For example, “rails generate model Book” or “rails generate model Subject” – you’re telling the generator to create models called Book and Subject to store instances of books and subjects. Notice that you are capitalizing Book and Subject and using the singular form. This is a Rails paradigm that you should follow each time you create a model.

When you use the generate tool, Rails creates the actual model file that holds all the methods unique to the model and the business rules you define, a unit test file for performing test-driven development, a sample data file (called fixtures) to use with the unit tests, and a Rails migration that makes creating database tables and columns easy.

Apart from creating many other files and directories, this will create files named book.rb and subject.rb containing a skeleton definition in the app/models directory.

Content available in book.rb −

class Book < ActiveRecord::Base
end

Content available in subject.rb −

class Subject < ActiveRecord::Base
end
25
Q

How do you create associations between Models?

A

When you have more than one model in your rails application, you would need to create connection between those models. You can do this via associations. Active Record supports three types of associations −

one-to-one − A one-to-one relationship exists when one item has exactly one of another item. For example, a person has exactly one birthday or a dog has exactly one owner.

one-to-many − A one-to-many relationship exists when a single object can be a member of many other objects. For instance, one subject can have many books.

many-to-many − A many-to-many relationship exists when the first object is related to one or more of a second object, and the second object is related to one or many of the first object.

You indicate these associations by adding declarations to your models: has_one, has_many, belongs_to, and has_and_belongs_to_many.

Now, you need to tell Rails what relationships you want to establish within the library data system. To do so, modify book.rb and subject.rb to look like this −

class Book < ActiveRecord::Base
   belongs_to :subject
end

We have used a singular subject in the above example, because one Book can belong to a single Subject.

class Subject < ActiveRecord::Base
has_many :books
end

We have used plural books here, because one subject can have multiple books.

26
Q

RSpec and shoulda-matchers:

What is shoulda-matchers?

A

Shoulda-matchers is an RSpec (not Cucumber!) extension library that allows us to test common Rails functionality, like validations and associations, with less code.

It’s basically a gem that EXTENDS (i.e. adds to) the already built-in RSpec matchers.

27
Q

What does the Factory Bot do?

A

factory_bot_rails is a library for setting up Ruby objects as test data. It allows you to create objects that are needed in tests without providing a value for each required attribute (or even creating the model for it). If you don’t provide a value for a required attribute factory_bot will use the default value that you defined in factory’s definition.

Definitions are created in the folder spec > factories.

28
Q

RSpec & testing:

What does adding the ‘–format documentation’ to the .rspec file do?

A

The –format option is to tell RSpec how to format its output.

By default, RSpec uses the progress formatter, which generates output like this:

….F…..*…..

’.’ represents a passing example, ‘F’ is failing, and ‘*’ is pending.

To instead see the documentation strings passed to each describe(), context(), and it() method, we add the documentation formatter:

–format documentation

29
Q

Cucumber & testing:

What does the database_cleaner gem do?

A

The database_cleaner gem is used to ensure a clean database state for testing. It gets rid of any garbage left over from interrupted or poorly-written tests—a common source of surprising test behavior.

30
Q

Cucumber & testing:

What do the following commands do?

db: create
db: migrate
db: test:prepare

A

Before testing, you will need to create the database and apply the migrations to your databases ( NOTE: a migration means that you move from the current version to a newer version. Using rake db:migrate you can apply any new changes to your schema. Essentially, to ensure latest version):

$ rails db:create
$ rails db:migrate db:test:prepare

  • db:create command creates a database using your current configuration. If RAILS_ENV is not specified it defaults to the development and test databases.
  • db:migrate command runs migrations for the current environment that have not run yet. By default it will run migrations only in the development environment.
  • db:test:prepare command checks for pending migrations and loads the test schema. (If you run rake without any arguments it will do this by default.)

FOOTNOTE:
rails db:migrate vs rake db:migrate

  • Rails core team decided to have consistency by enabling rails command to support everything that rake does. And db:migrate which is a rake command is now being supported by rails command. In this case db:migrate, Rails delegates the execution to Rake via Rake Proxy.
31
Q

Setting up a Rails project:

What are ‘generators’?

A

Rails comes with a number of scripts called generators that are designed to make your development life easier by creating everything that’s necessary to start working on a particular task. One of these is the new application generator, which will provide you with the foundation of a fresh Rails application so that you don’t have to write it yourself.

32
Q

What does the Routing do in Rails?

A

While a controller’s purpose 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.

33
Q

Setting up a Rails project:

What does the routes.rb file do? Give examples of how to configure it.

A

The routes.rb file (in the config folder) is where you tell Rails how to direct incoming requests to controllers and actions. Think of creating routes as drawing a map for your incoming requests. The map tells them where to go based on some predefined pattern, like this:

Rails.application.routes.draw do
  Pattern 1 tells some request to go to one place
  Pattern 2 tell them to go to another
  ...
end

Examples:

Rails.application.routes.draw do
   get 'books/list'
   get 'books/new'
   post 'books/create'
   patch 'books/update'
end
34
Q

Setting up a Rails project:

What is a ‘resource’?

A

A resource is the term used for a collection of similar objects, such as articles, people or animals – think of it as a bucket of similar things. You can create, read, update and destroy items for a resource and these operations are referred to as CRUD operations.

Rails provides a resources method which can be used to declare a standard REST resource. E.g., in the case of object Articles, you need to add the article resource to the config/routes.rb so the file will look as follows:

Rails.application.routes.draw do
get ‘welcome/index’

resources :articles

root ‘welcome#index’w
end

If you then run the command rails routes, you’ll see that it has defined routes for all the standard RESTful actions.

35
Q

Setting up a Rails project:

How do you create a controller?

A

To create a new controller, you will need to run the “controller” generator and tell it you want a controller called e.g. “Welcome” with an action called “index”, just like this:

$ rails generate controller Welcome index

NOTE1: this will create several files for you, but most importantly the controller itself and a related view, each located at:

app/controllers/welcome_controller.rb
app/views/welcome/index.html.erb.

NOTE2: when controllers are generated in Rails they are empty by default, unless you tell it your desired actions during the generation process. So it’ll look like this:

class WelcomeController < ApplicationController
end
****
NOTE3: a controller is simply a class that is defined to inherit from ApplicationController. It's inside this class that you'll define methods that will become the actions for this controller. These actions will perform CRUD operations on the articles within our system.
36
Q

Setting up a Rails project:

What is CoffeeScript?

A

CoffeeScript is a small programming language that transcompiles (converts) to JavaScript. It adds syntactic sugar inspired by Ruby, Python and Haskell in an effort to enhance JavaScript’s brevity and readability. CoffeeScript support is included in Ruby on Rails.

37
Q

Setting up a Rails project:

When you define a new action in a controller and then run a test, Rails will tell you a template is missing. E.g. by defining “new” method in ArticlesController. You then have to create a template likes this:

app/views/articles/new.html.erb

Which part of that path is the format and which is the ‘handler’?

A

The first extension is the format of the template and the second extension is the handler that will be used to render the template. The format for this template can only be html and the default handler for HTML is erb.

NOTE: Rails uses other handlers for other formats. E.g. coffee handler uses CoffeeScript to build JavaScript templates. But in this case, since you want to create a new HTML form, you will be using the ERB language which is designed to embed Ruby in HTML.

38
Q

Setting up a Rails project:

What is Ajax and what does it do as it relates to e.g. forms?

A

Ajax stands for Asynchronous JavaScript and XML. Ajax is not a single technology; it is a suite of several technologies. Ajax incorporates the following −

XHTML for the markup of web pages
CSS for the styling
Dynamic display and interaction using the DOM
Data manipulation and interchange using XML
Data retrieval using XMLHttpRequest
JavaScript as the glue that meshes all this together

Ajax enables you to retrieve data for a web page without having to refresh the contents of the entire page. In the basic web architecture, the user clicks a link or submits a form. The form is submitted to the server, which then sends back a response. The response is then displayed for the user on a new page.

When you interact with an Ajax-powered web page, it loads an Ajax engine in the background. The engine is written in JavaScript and its responsibility is to both communicate with the web server and display the results to the user. When you submit data using an Ajax-powered form, the server returns an HTML fragment that contains the server’s response and displays only the data that is new or changed as opposed to refreshing the entire page.

39
Q

Setting up a Rails project:

How do you create a Model?
What is the naming convention for a Model (singular vs plural)?

A

Rails provides a generator for creating models. To create a new model ‘Article’ with e.g. the attributes of ‘title’ and ‘text’ , run this command in your terminal:

$ rails generate model Article title:string text:text

Models in Rails use a singular name, and their corresponding database tables use a plural name.

NOTE: Active Record is smart enough to automatically map column names to model attributes, which means you don’t have to declare attributes inside Rails models, as that will be done automatically by Active Record.

40
Q

Setting up a Rails project:

What are migrations?

A

Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it’s possible to undo a migration after it’s been applied to your database.

Migration filenames include a timestamp to ensure that they’re processed in the order that they were created.

NOTE1: for example, in the migration file db/migrate/YYYYMMDDHHMMSS_create_articles.rb, you’ll find:

class CreateArticles < ActiveRecord::Migration[5.0]
  def change
    create_table :articles do |t|
      t.string :title
      t.text :text
  t.timestamps
end   end end

The above migration creates a method named ‘change’ which will be called when you run this migration. When you run this migration it will create an articles table with one string column and a text column. It also creates two timestamp fields to allow Rails to track article creation and update times.

NOTE2: because you’re working in the development environment by default, this command will apply to the database defined in the development section of your config/database.yml file. If you would like to execute migrations in another environment, for instance in production, you must explicitly pass it when invoking the command: bin/rails db:migrate RAILS_ENV=production.

41
Q

General lingo:

What does “white listing” mean? How does it relate to ‘strong parameters’?

A

Add to a list of people or things considered trustworthy. E.g. with strong parameters, you are white listing exactly which parameters your application will accept.

42
Q

Setting up a Rails project:

What are strong parameters and how do they help with securing applications?

A

With strong parameters, Action Controller parameters are forbidden to be used in Active Model ‘mass assignments’ until they have been whitelisted. Basically you tell Rails exactly which parameters are allowed into our controller actions.

E.g., what if a request to the server was crafted to look like a new article form submit but also included extra fields with values that violated your application’s integrity? They would be ‘mass assigned’ into your model and then into the database along with the good stuff - potentially breaking your application or worse.

So you have to whitelist your controller parameters to prevent wrongful mass assignment. In this case, you want to both allow and require the title and text parameters for valid use of create. The syntax for this introduces require and permit.

@article = Article.new(params.require(:article).permit(:title, :text))

43
Q

Setting up a Rails project:

What is the best practice for how to order standard CRUD actions in the controller, i.e. in which order should they be listed?

A

A frequent practice is to place the standard CRUD actions in each controller in the following order: index, show, new, edit, create, update and destroy.

44
Q

Setting up a Rails project:

When setting up a model (class) and it inherits from ActiveRecord::Base, what does it really inherit?

A

By inheriting from ActiveRecord::Base, the model (class) receives great deal of functionality for free, including basic database CRUD (Create, Read, Update, Destroy) operations, data validation, as well as sophisticated search support and the ability to relate multiple models to one another.

45
Q

Setting up a Rails project:

In testing, if the database is corrupted and you need to reset/re-create it to get a start fresh, what command line do you run?

A

rake db:drop db:create db:migrate

46
Q

Setting up a Rails project:

What is Active Storage?

A

Active Storage is a tool for managing file uploads. Files can be attached to models, transformed using tools such as ImageMagick, and stored in a file on the server or a cloud storage service like Amazon S3, Microsoft Azure Storage, or Google Cloud Storage. It provides an easy-to-use API interface that feels familiar if you already know other Rails tools like ActiveRecord.

47
Q

Setting up a Rails project:

What is the Selenium webdriver and what does it help you to do?

A

Selenium WebDriver tool is used to automate web application testing to verify that it works as expected. It supports many browsers such as Firefox, Chrome, IE, and Safari.

It basically allows you to “drive” a browser natively as a user would, allowing you to test interactive/dynamic features as well, i.e. javascript features.

48
Q

General concept:

What’s the difference between data and information?

A

Information is data put into context.

E.g. the number 41 is just pure data, but the logic layer can then give you 41 as an age, meaning that number is now information.

49
Q

Setting up a Rails project:

What are the data migration files?

A

They are basically blueprints. Given that the actual databases (e.g. PostGRES) are only stored locally when you are developing and not included when you upload / share the app with someone, the db migration files act as a blueprint to instruct the receiving end what db structure to set up.

50
Q

Two things you ought to do each time you do a git pull of new code (i.e. when many ppl working on the code?

A

bundle + db:migrate

To install any new gems and make any schema updates

51
Q

What is the schema

A

A representation of your db, i.e. a snapshot of it w/o giving it direkt access.

52
Q

General concept:

What does ‘persist’ mean, in the context of data?

A

persist = save / store

53
Q

General concept:

What does “mass assignment” mean in the context of params?

A

“mass assignment” in that you are assigning multiple values to attributes via a single assignment operator. So you need to use strong params.

In contrast, if you are assigning just one attribute, you can skip strong params.

54
Q

Rails testing:

What do Semaphore and Coverall do?

A

Semaphore

  • A centralised place to run all the commands (e.g. rake, db:create db:migrate), tests, etc. for your code to ensure it’s all green (pass). You can view its log while it’s running to see which commands it runs.
  • Why we need Semaphore if tests already run green on our own local tests? Because e.g. Hanna can have different set ups that allow the tests to go green on her computer while it won’t pass on Camron’s computer. But once all green on Semaphore, it’ll go green on everyone’s computer (meaning you have all configs etc. in the code correctly).
  • Essentially, Semaphore is a secondary, centralised filter to ensure all-passing code.

Coverall

  • “We’ll make use of Coveralls a web service that helps track code coverage. You can use it for various programming languages and for Ruby, there is a gem we can use.”
  • Code coverage: tells you how many of lines of your code is being tested by our own tests (e.g. rspec and cucumber). E.g. if you have 1000 lines of code and 900 lines are tested by your tests, you’ll have a 90% test coverage
55
Q

In general, what is the workflow when creating a rails app (in terms of what is created, connecting with what, etc)?

A

In general, using the request/response cycle as a guide, the following is the workflow when making a Rails app:

-Generate a new Rails app.
⁃Generate a controller and add an action.
⁃Create a route that maps a URL to the controller action.
⁃Create a view with HTML and CSS.
⁃Run the local web server and preview the app in the browser.