Basics Flashcards

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

What is the request/response cycle?

A

The browser makes a request for the URL http://localhost:3000

The request hits the Rails router in config/routes.rb.

The router recognizes the URL and sends the request to the controller.

The controller receives the request and processes it.

The controller passes the request to the view.

The view renders the page as HTML.

The controller sends the HTML back to the browser for you to see.

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

How do you make a controller?

A

rails g controller Name

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

What is the workflow when making a rails app?

A

1) Generate a new rails app = rails new App_name
2) Generate a controller and add an action = rails g controller action
3) Create a route that maps a URL to a controller action
4) Create a view with HTML and CSS
5) Check it in the localhost with rails s

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

How do the model and database fit into the request/response cycle?

A

When the controller action receives the request it send for the data to the model which in turn brings the data from the database and sends it back to the action.

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

How do you create a model?

A

1) rails g model Name title:string description:text price:integer
2) run rails db:migrate
3) rails db:seed

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

What does the model do?

A

It represents a table in the database

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

What is a migration?

A

A migration is a way to update the database.

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

What does the migration file contain?

A

1) It contains the change method which tells rails what change to make in the database
2) Inside the change method, we have the create_table method which creates a new table in the database for storing.
3) Inside the create table we have a title which creates a string and so on.
4) We also have the timestamps which automatically creates a created_at and updated_at

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

What does rails db:seed do?

A

The command seeds the database with sample data from db/seeds.rb.

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

What is the gemfile

A

The gemfile contains all the gems that your app uses.

With the source on the top that are housed in rubygems.org]

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

What is bundle install?

A

Bundle goes to the source and brings the gems that you have in your gemfile and then installs them

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

How is the gemfile grouped?

A

group development
group development test
group production
you place the gems depending on which environment you are using

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

What is the gemfile.lock?

A

The bundler creates the gemfile.lock adds the dependencies that are needed. Never update this manually.

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

What is the database.yml?

A

It says what databased are used and in what environment

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

What are migrations?

A

files that we use to create or update databases.

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

What is a class?

A

A collection of methods or functionality

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

What is snake case?

A

snake_case.rb

All file names should be in snake case

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

What is a CamelCase

A
All class names should be CamelCase
ApplicationController
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is an action?

A

An action is a fancy name for a method.

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

What is a view?

A

A view is everything you see in your browser.

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

What does erb stand for?

A

embedded ruby all html files in rails are called name.html.erb

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

What is the application.html.erb file?

A

The application.html.erb file is our wrapper file, that means that all of our view files are a part of that file. All files are displayed through that file.

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

What is the inside the application.html.erb file?

A

This is where all your views show up from the other views.

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

What is the naming convention for the controller files?

A

Controller file names need to be snake_case

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

What do you need if you want a view from a controller?

A

You need to have a folder with the controller name under views if you want to have views. views/pages/filename

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

What do you need if you want the views to create a view?

A

You need a action(method) defined and a html.erb file added to it under the views.

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

How do you write a route?

A

get ‘/thispage’, to: ‘controller#action’

Get the route and send it to a controller with a action(method)

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

What is the naming convention for model names?

A

A model name should be singular Model model.rb

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

What is the naming convention for table names?

A

Tables should be in plural beacuse we have a lot of tables. tabels table

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

What is the naming convention for controller names?

A

They should be in plural todos_controller.rb

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

How do you create a migration file?

A

First you need to have a data model (model file)

rails generate migration create_todos

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

How do you run a migration?

A

rails db:migrate

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

Why do you run a migration file?

A

To create a table and actually impact the database i need to run a migration.

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

How do you interact with the database?

A

You interact with the database with rails console.

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

How do you create a database object?

A

In rails console
Initiate a new object
save the object

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

What are the steps to make a new object in the database?

A
1) object = Class.new(name: "something", description: "else")
2 object.save
The class is the model name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

How do you see what is saved in the database?

A

ClassName.all

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

What does the .create do in the database?

A

the .create will impact the database as long as there are no errors. you do not need to use.new and .save

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

How do you find a object with a known id in the database?

A

some_variable_name = Class.find idnumber
example:
todo1 = Todo.find 2

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

How do I update a object in a database from the console?

A

1) variable_name.what_you_want_to_change = “Changes”
2) variable_name.save

example
todo1.description = “eat something”

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

How do I delete a object in the console?

A

1) variable_name = Class.last
2) varible_name.destroy

example
todo2 = Todo.last
todo2.destroy

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

How do I make sure that there are no empty objects in the database?

A

You add

validates :name(or other), presence: true in the model class

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

How do I see the error messages from rails console?

A

variablename.errors.any?
and
variablename.errors.full_messages

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

How do you get all the CRUD routes?

A

with adding resources :classname in the route

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

What is the purpose of the rails router?

A

It has two puorposes

1) Send a URL request to the right controller
2) Generate paths and URLS to avoid hardcoding strings in the views.

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

How do you connect a URL to code in the rails routes?

A

If the incoming request is
GET /patient/17

This means that your route should be

get ‘/patient/:id, to ‘patient#show’

In plain english
get the route from patient id and send it to the patient controller and the show action(method)

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

How do you generate a path or url from the code?

A

If you have this code in the controller:
@patient = Patient.find(17)

And this code in the view:

Then you type this in the route:
get ‘/patients/:id’, to: ‘patients#show’, as: ‘patient’

48
Q

What is resource routing?

A

Resource routing is when you declare all the crud functions routes for a controller with a single line of code.

49
Q

How do you use resource routing?

A

you type:

resources :controllername

50
Q

How can you have resources for multiple controllers?

A

by typing them after the first controller:

resources :photos, :books, :videos

51
Q

What is flash’?

A

Flash is essentially a hash which I can add messages and then display the messages.

52
Q

How do you find a object from a class?

A

@instance_variable = Class.find(params[:id])

53
Q

What is the preferred way to iterate in Ruby?

A

It is by using the .each method.

54
Q

How do you add links to the view?

A

with the _path method

you write :

you find the prefix in rails routes

55
Q

What are partials?

A

Partials allow you to organize and reuse your code in your rails application. It’s a way to keep your code DRY

56
Q

How to you create partials?

A

you create partials in the folder you need with the _partialname.html.erb

57
Q

How do you insert a partial in your code?

A

note:
you only need the folder if the partial is in another folder.
no _ in the beginning of the partial name nor a format name like html.erb in the end.

58
Q

How do you test if the routes you created work?

A

You type rails routes in the terminal and see if they are displaying in the terminal.

59
Q

How do you install bootstrap in rails?

A

You add the gem bootstrap
1) sass to your Gemfile
2) run bundle
3) add //= require bootstrap-sprockets under require jquery
4) create a new css.scss file under stylesheets.
5) In the scss file add
@import “bootstrap-sprocets’;
@import “bootstrap”;
Check if the gem has been properly installed by seeing if the font has changed

60
Q

What are relational databases?

A

It is a database structure based on rows and columns and their relationship

61
Q

What is SQL?

A

It stands for structured Queryt Language and is the language used to manage and maintain databases.

62
Q

What is Active Record?

A

Active record lets us manage databases without using SQL in Rails. Active Record takes ruby code and translates it to SQL

63
Q

What is RDBMS?

A

Relational Database Management System

such as MS SQL, Oracle, MySQL

64
Q

How is data stored in a database?

A

The data is stored in objects called tables.

65
Q

How are database tables structured?

A

They are structured in rows and columns

66
Q

What is a record?

A

A record is another name for a row (horizontal)

67
Q

What are SQL statements?

A

All actions you can perform to a database are done with SQL stetements

SELECT * FROM Customers:

68
Q

What are the most important SQL Commands?

A
  • SELECT -Extracts data from a database
  • UPDATE -Updates data in a database
  • DELETE -Deletes data from a database
  • INSERT INTO -inserts new data into a database
  • CREATE DATABASE -creates a new database
  • ALTER DATABASE -modifies a database
  • CREATE TABLE -creates a new table
  • ALTER TABLE -modifies a table
  • DROP TABLE -deletes a table
  • CREATE INDEX -creates a index (search key)
  • DROP INDEX -Deletes an index
69
Q

What are groups in the Gemfile?

A

The groups give you the option to perform actions on an entire group.
We also group our gems to make sure that they are used in the right environment.

70
Q

What is rake?

A

Rake is a gem that allows us to execute commands.

71
Q

Where can I find the Rake commands?

A

They are in the rakefile

72
Q

What is rake db:create?

A

It creates an empty database in your application.

73
Q

What is rake db:drop?

A

It drops your database in your application

74
Q

What is rake db:migrate?

A

it gives your database a structure. Creating tables and adding columns.

75
Q

How do you generate a new Rails app?

A

rails new AppName -options

76
Q

How do you start the rails server?

A

in the terminal

$ rails s

77
Q

How do you create a database?

A

in the terminal

$ rails db:create

78
Q

What is a scaffold in rails?

A

A scaffold is the ability to generate multiple items at the same time.

79
Q

How do you create a scaffold in rails?

A

terminal

$ rails g scaffold FeatureName attribute(like title):datatype(like string)

80
Q

Should you change the schema.rb file?

A

NEVER

81
Q

What is a before_action in the controller?

A

The before_action is a method that is run before a controller action.
You use the before action to avoid duplicate code.

82
Q

What is an action in rails?

A

An action is a ruby method inside a controller

83
Q

What is the difference between the new and create action?

A

the new action instantiates a new object and sends you to the form.
the create action creates the blogpost

84
Q

What are resources inside the routes.rb?

A

the resources keyword gives us access to all the associated routes like create, new, edit destroy, index

85
Q

What are initializers in the config folder?

A

Initializers are files that will be run before the application starts.

ex
you want to connect to an API before the app starts.

86
Q

How do you add files to gitignore?

A
1) add a comment and add
/folder/file.rb
2) $ git status
3) $ git rm . -r --cached (remove all cache)
4) git status
87
Q

What does a migration do?

A

The migration changes the schema of the database.

Meaning that the migration changes the database to our new instructions.

88
Q

What is the rails console?

A

The console is a direct connection to the database

89
Q

What is a seed file?

A

The seed file allows you to create sample data. You can run ruby code inside it

90
Q

How do you use the seed file?

A

you run

$ rails db:setup #clears all data and replaces it with the content from the seed file

91
Q

How do you create an index action?

A

An index action is used when you want to list a number of items and perhaps display them.

1)
class SomeClass < ApplicationController
def index
  @some_items = ModelClass.all
end
end

2)

and create a view (template) under the folder
3)
and add

92
Q

How do you search and find specific routes?

A

rails routes | grep search_term

93
Q

What is the new action?

A

The new action displays the form where you enter the information, it is saved and completed in the create action
(only renders the form)

94
Q

What is the create action?

A

The create action creates and saves the information added through the form in the new action.

95
Q

How do we create the new method?

A
class SomeClass < ApplicationController
  def new
    @some_item = ModelClass.new
  end
end

And create a view under the right folder and add a form.

96
Q

What does the error message “First argument in form cannot be nil or empty” mean?

A

It means that the instance variable is missing.

97
Q

How do you create a the create method?

A
def create
@some_item = ModelClass.new(params[:some_item])
    if @some_item.save
      redirect_to @some_item
    else
      render "new"
    end
  end
end
98
Q

What is routing?

A

It decides which controller receives which request.

There are often more routes to each controller and they g to different actions

99
Q

What is the purpose of the controller?

A

It’s purpose is to recive a specific request for the application.

100
Q

What is an action?

A

An action is a method, its purpose is to collect information to provide to the view.

101
Q

What is a view?

A

A views purpose is to display information in a human readable way. it gathers its information from an action.

102
Q

How do you create an controller?

A

$ rails g controller Welcome index

Welcome being the controller name and index being the action (method)

103
Q

What files does a generate controller make?

A
The command generates:
a controller
a view folder
a view (named as the action)
a route 
a test folder
a test file
a helper folder
a helper file
and stylesheets
104
Q

How do you set a root path?

A

in config/routes
add
root ‘controller#action’

105
Q

What is a helper method?

A

A helper method is a method written for the view.

it’s designed to be reusable anywhere on the website, and it cleans up the view code by separating out some logic.

106
Q

What is the null object pattern?

A

a null object is an object with no referenced value or with defined neutral (“null”) behavior. The null object design pattern describes the uses of such objects and their behavior (or lack thereof).

107
Q

What are view helpers?

A

they are similar to partials,
They can be called from anywhere in the application
helpers are written in ruby, but partials are written in html

108
Q

How do you check that your new rails app is properly installed?

A

You run the $ rails about and then run the $ rails s command

109
Q

What are binstubs?

A

They are wrapper scripts that prepare the environment so that the startup time decreases. It also makes sure that you are running the correct version of ruby.

110
Q

How do you give access to other computers to your rails server?

A

You need to specify the localhost to 0.0.0.0
by running the command
$ rails server -b 0.0.0.0

111
Q

What does the $ rails g controller command create?

A

It creates

  • a controller file,
  • routes to the actions
  • a view folder and views files for the actions.

It also creates styling files and test files.

112
Q

How do we add dynamic content to our templates?

A

We add dynamic content with embedded ruby

113
Q

How do you link to different views in rails?

A

You use the link_to helper method

114
Q

How can you see all tasks in rails?

A

with $ rails -T [option]
for example
rails -T db => gives you a list of all db tasks

115
Q

How can you see the migration status?

A

with the command:

$ rails db:migration:status

116
Q

What is the number_to_currency method?

A

The number _to_currency method is a view helper that allows you to format a number into a currency

For example:
  Gross: %= number_to_currency(movie.total_gross, 
# options:
precision: 0, # decimals
unit: 'SEK',  # defaults to $
format: '%n %u' # Format of currency 
) 

%>

=> Gross: 200,000,000 SEK

117
Q

How do you clear cache in assets?

A

rails tmp:clear