Rails Flashcards
What is a router?
Router is something in rails that is supposed to determine who to send the http request to. Router takes the path and the method of the request into consideration. (i.e. path = users, method: get). What combinations of paths and methods are associated with which controllers and which actions of these controllers
What is a controller?
Responsible for controlling one resource. (users controller processes all requests that are about users). Fill out http response and give it back to the router
What are controller actions?
Methods defined on the controller. Router when it matches the path and the method.. Will initialize the controller and call the right action. So, if the router knew a path of users and a method of get was supposed to match the users controller and index action. it would then create a new instance of the users controller and then call the index action
What is a routes.rb file?g
checks the list for path and methods to process http request.
Lives in config/routes.rb
Define name of html method and specify path its supposed to match
i.e. - get ‘superheroes’, to: ‘superheroes#index’
What are views?
Dynamically generate html based on some information we get from our database.
What does REST / RESTful mean?
Representational state transfer. An agreed upon way to specify these routes, so that people can trust that it’s going to do the same thing across applications.
You can create resources: to create all restful routes
How to create a database on Rails
Run bundle exec rails db:create
If you have migrations, run bundle exec rails db:migrate
If you have seeds, run bundle exec rails db:seed
What is a GET Request?
Without a wildcard (:id) It returns all entries in the database. With a wildcard it returns the entry in the database with the matching id in the wildcard
What is a POST request?
Creates an entry in the resource
What is a PATCH request?
Updating an entry in the resource with the given id. it is similar to put and you want to have both routes.
What is a DELETE request?
Removes an entry from the resource with the given ID
What are controller params?
Sort of an instance method in controllers. It is a has like object given to the controller by the router. It contains 3 things:
1) Query string
2) Request body
3) URL params / route params
When the route is matched the router will take the pieces above and will parse them. It will populate the params object with the key-value pairs that match accordingly. Now, Controller can use these params as a hash. You can treat params hash like a normal hash.