Review Questions Weeks 1-3
MVC & Rails
Diagram and explain the MVC model
- controller: Coordinates the response to an HTTP request and parses user requests, data submissions, cookies, sessions.
- models: Contains all business logic and contains data for the application.
- view: Templates for pages that we will display to our user. data placeholders.
- DMed diagram via slack.
What is the role of the model?
- Interacts with and reflects a table in a database. It also sets validations, methods and other attributes.
When would you want to use a class method rather than an instance method?
- You use class methods when the functionality you are writing does not belong to an instance of that class.
- You use instance methods when manipulate or use a particular instance of the class.
What is the role of a view? What language(s) would you use in a view?
- Template for the http response to render the information for the user ,languages used are html and erb(embedded ruby).
What is the role of the controller?
- Passes data between the model and the view, and sends the output back to the user. What are the 7 RESTful CRUD actions?
- index, edit, update, show, new, create, destroy
Create a chart for Users with columns for path_helpers, HTTP Verb, URI, ActiveRecord, Redirect/Render, View.
path_helpers | HTTP | Verb | URI | ActiveRecord | Redirect/Render | View
users_path | | get |/users |users = User.all | render |index
user_path(user) | | get |/users/:id |user = User.find(user_params) | render |show
new_user_path | | get |/users/new |user = User.new | render |new
edit_user_path(user | | get |/users/:id/edit |user = User.find(user_params) | render |edit
user_path(user) | |put/patch|/users/:id/update|user = User.find(user_params) | redirect |show
user.update
users_path | | post |/users |user = User.create(user_params)| redirect |show/index
|user.save
user_path(user) | | delete |/users/:id |user = User.find(user_params) | redirect |index
|user.delete
HTTP
What are the 5 HTTP Verbs?
- get, post, put, patch, delete.
Diagram and explain the HTTP request/response cycle.
- DMed diagram via slack.
HTML/CSS
What HTML tags would you use for the following? section header:
- general content:
<p>..</p>
- bullet points:
<ul>..</ul>
- numbered list:
<li>..</li>
- image:
<img>..</img>
- link:
<a href..</a>
form & its pieces
How do you target the following in CSS?
a class: .class
an id: #id
an element: *
Testing
How would you set up a test to check that a User has an email address and that only one user can have that email address? in the model I validate that the email is unique.
class Fu < ActiveRecord::Base
validates_uniqueness_of :email
end
# spec
describe Fu do
it "should have a unique email" do
Fu.create!(:email=>"Fu@bar.com")
fu = Fu.new(:email=>"Fu@bar.com")
fu.should_not be_valid
fu.errors[:email].should include("has already been taken")
end
end
How would you set up a test to check that a specific User is listed on a page?
- expect(page).to have_content("#{user.name}")
Databases
What is the SQL to pull the record for every User?
SELECT * FROM users;
What is the SQL to pull the record for how many Users are in the database?
SELECT COUNT(id) FROM users;
Diagram the tables of a database where a User has_many Assignments, an Assignment belongs_to a User, a Course has_many Assignments, and an Assignment belongs_to a Course.
- DMed diagram via slack.
ActiveRecord
When would I use a find, vs a find_by, vs a where?
- find only finds by the id and find_by finds the first object by any column/att. where finds all records that match the specified attribute.
What ActiveRecord methods would I use in each of the 7 CRUD actions?
- index: all
- new: new
- create: new, save or create
- edit: find
- update: find, update
- destroy: find, delete
- show: find
How do I make it so I can call user.assignments? What about assignment.users?
- in the user model
has_many :assignments
and in the assignments modelhas_many :users
.