Basics Flashcards
What is the request/response cycle?
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 do you make a controller?
rails g controller Name
What is the workflow when making a rails app?
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 do the model and database fit into the request/response cycle?
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 do you create a model?
1) rails g model Name title:string description:text price:integer
2) run rails db:migrate
3) rails db:seed
What does the model do?
It represents a table in the database
What is a migration?
A migration is a way to update the database.
What does the migration file contain?
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
What does rails db:seed do?
The command seeds the database with sample data from db/seeds.rb.
What is the gemfile
The gemfile contains all the gems that your app uses.
With the source on the top that are housed in rubygems.org]
What is bundle install?
Bundle goes to the source and brings the gems that you have in your gemfile and then installs them
How is the gemfile grouped?
group development
group development test
group production
you place the gems depending on which environment you are using
What is the gemfile.lock?
The bundler creates the gemfile.lock adds the dependencies that are needed. Never update this manually.
What is the database.yml?
It says what databased are used and in what environment
What are migrations?
files that we use to create or update databases.
What is a class?
A collection of methods or functionality
What is snake case?
snake_case.rb
All file names should be in snake case
What is a CamelCase
All class names should be CamelCase ApplicationController
What is an action?
An action is a fancy name for a method.
What is a view?
A view is everything you see in your browser.
What does erb stand for?
embedded ruby all html files in rails are called name.html.erb
What is the application.html.erb file?
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.
What is the inside the application.html.erb file?
This is where all your views show up from the other views.
What is the naming convention for the controller files?
Controller file names need to be snake_case
What do you need if you want a view from a controller?
You need to have a folder with the controller name under views if you want to have views. views/pages/filename
What do you need if you want the views to create a view?
You need a action(method) defined and a html.erb file added to it under the views.
How do you write a route?
get ‘/thispage’, to: ‘controller#action’
Get the route and send it to a controller with a action(method)
What is the naming convention for model names?
A model name should be singular Model model.rb
What is the naming convention for table names?
Tables should be in plural beacuse we have a lot of tables. tabels table
What is the naming convention for controller names?
They should be in plural todos_controller.rb
How do you create a migration file?
First you need to have a data model (model file)
rails generate migration create_todos
How do you run a migration?
rails db:migrate
Why do you run a migration file?
To create a table and actually impact the database i need to run a migration.
How do you interact with the database?
You interact with the database with rails console.
How do you create a database object?
In rails console
Initiate a new object
save the object
What are the steps to make a new object in the database?
1) object = Class.new(name: "something", description: "else") 2 object.save The class is the model name
How do you see what is saved in the database?
ClassName.all
What does the .create do in the database?
the .create will impact the database as long as there are no errors. you do not need to use.new and .save
How do you find a object with a known id in the database?
some_variable_name = Class.find idnumber
example:
todo1 = Todo.find 2
How do I update a object in a database from the console?
1) variable_name.what_you_want_to_change = “Changes”
2) variable_name.save
example
todo1.description = “eat something”
How do I delete a object in the console?
1) variable_name = Class.last
2) varible_name.destroy
example
todo2 = Todo.last
todo2.destroy
How do I make sure that there are no empty objects in the database?
You add
validates :name(or other), presence: true in the model class
How do I see the error messages from rails console?
variablename.errors.any?
and
variablename.errors.full_messages
How do you get all the CRUD routes?
with adding resources :classname in the route
What is the purpose of the rails router?
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 do you connect a URL to code in the rails routes?
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)