Generators in RoR are a great way to quickly create folders and files. Could you name a few? What are the specific differences between them?
Migration - makes a migration file
Controller - makes views, controller
Model - DB/Model
Resource - DB/Model/view folder/controller file/ routes
Scaffold - EVERYTHING (which isn't good!!)
What are some of the methods that we get from the belongs_to macro? For example, if we say `belongs_to :user`, what are methods we now have access to?
.build_user
.user =
.user
. create_user
What methods and validations do we get from has_secure_password?
.password =
.authenticate
will validate that it has a password before saving to the database.
How can you check the list of all the routes you created?
What determines the keys in a params hash? (there are 2 answers to this - think about routes AND forms)
Route: the URL keys ex. :id or :genre_id
Form: the symbol being passed through the form. ex. f.text_field :name => the key would be name.
If you wanted to add an additional attribute to a model, how would you do that?
with a new migration => add column
What does the .build method do and can you give an example of when to use it?
.build makes an object with an association but without saving it to the database.
- you can use it when making a nested form for fields_for OR you can use it for a nested routed form to associate the data in the controller.
How can you tell if a path helper needs an argument?
by if it has an id in it's URL path ex. :id or :bood_id, some need more than one passed in (some nested routes)
How do you make a custom route?
ex. get '/home', to: 'sessions#home'
make sure to write it above your other routes.
What are strong params and why do we use them?
Strong Parameters is a feature of Rails that prevents assigning request parameters to objects unless they have been explicitly permitted. (verifies that we are only passing in the things we want)
We use them to prevent the user from creating or editing things we do not want them too.
What’s the difference between `.new` and `.create`?
new created a new object but doesn't save it to the database.
create will make a new object and save it to the database.
How do you set up a many-to-many relationship?
with a joins table and using has_many through in your model for the many to many association.
What is the difference between a POST request and a GET request?
GET is used for viewing something, without changing it, while POST is used for changing/creating something.
What are all the routes we get when we type in `resources :users`?
new, create, update, edit, index, show, destroy (all for user)
hen would you use form_for and when would you use form_tag? How about form_with?
Form_Tag would be when you are not creating an object, so something like a login or a search.
Form_For is when you are creating or updating an object.
How do model validations work in general?
they make sure your database doesn't get "bad data" it will check the validations when it tries to save the object to the database.
How can you tell if a table is a join table?
two foreign keys
When should you use helpers? When should you use partials?
helpers are to extract complex logic out of the views.
partials are used for big chunks of duplicate code.
Why do we use render after a failed attempt to create/update an object?
To autofill the data that we got correct so the user doesn't have to completely refill out the form. so instead of using a blank object for the form it will used the one that got made but failed to save to the database.
How does form_for know where to send your POST request when you hit ‘submit’?
The form_for helper interprets the model given to it (ex. @post => /posts) and uses rails conventions to calculate the route.
Which methods will trigger model validations to run? Could you name a few methods that won’t?
What is the difference between using build for a has_many relationship vs. a belongs to relationship? Why do we need a custom build for a belongs to?
ex a library has many books
library.books.build
=> If library.books gives us an empty array we can still chain commands on without breaking
book.build_library
=> if book.library is empty it returns nil. We can not chain things onto nil it will just break and give us a no method for nil class error. So a custom method is needed.
When should you use scope methods?
When you want to query a database, ex. something like a filter or ordering a database in your views.
How do you set-up nested routes? Why would you use a nested route?
you create a block and nest them. ex.
resources :games do
resources :reviews
end
you would use nested routes for create associated data that is already associated to the parent element or to show associated data in the route.
Can we create more than one resource/object from the context of the same form? If so, what are the necessary steps?
Yes, a nested form using either a custom writer or accepts_nested_attributes_for, and the fields_for helper.