Chapter 2 Flashcards
What is a scaffold generator?
Part of Rails which generates code and thereby functionality
How can we generate a new Rails app using a specific version of Rails?
$ rails railsversionnum new appName
What is a data model?
A representation of the structures needed by our application
What is one way to approach creating a data model?
Identify WHO and WHAT will be needed in the app (ex. users & microblogs), and break down the data/data types needed for each object.
Explain the following command:
’$ rails generate scaffold User name:string email:string
This generates a scaffold by passing the ‘scaffold’ command to the ‘rails generate’ script.
The argument of the scaffold command is the singular version of the resource name (in this case, User), together with optional parameters for the data model’s attributes (where the syntax is dataName:dataType)
Note that there is no need to include a parameter for id; it is created automatically by Rails for use as the primary key in the database.
Lastly, note that ‘User’ is SINGULAR! Rails will make it plural by adding an ‘s’ where necessary.
Why bother adding ‘bundle exec’ to the following command?
’$ bundle exec rake db:migrate’
‘bundle exec’ ensures that the Rake version specified in our Gemfile is used
After creating an object through scaffolding, what is the next step?
Update our database with the new model via a database migration.
Having made a model through scaffolding, how can we access it in our browser?
from the root URL, go to /modelName to see the contents of the database and a dialog to edit it.
/modelName/1 - go to user with ID of 1
Articulate the process of a typical browser hit to the index page /users using the MVC model
- The browser issues a request for the /users URL.
- Rails routes /users to the index action in the Users controller.
- The index action asks the User model to retrieve all users (User.all).
- The User model pulls all the users from the database.
- The User model returns the list of users to the controller.
- The controller captures the users in the @users variable, which is passed to the index view.
- The view uses embedded Ruby to render the page as HTML.
- The controller passes the HTML back to the browser.
What does the following do in the routes.rb file?
resources :users
Maps the /users URL to the controller actions for the ‘Users’ resource. This tells the Rails router, which receives a request from a browser, where to route the request to the proper controller action based on the URL
What is the definition of a controller in Rails?
A collection of related actions (functions)
What is the REST architecture?
The ideas of representational state transfer as identified and named by computer scientist Roy Fielding for developing distributed, networked systems and software applications such as the World Wide Web and web applications.
Explain REST in the context of Rails.
Most application components (such as users and microposts) are modeled as resources that can be created, read, updated, and deleted—operations that correspond both to the CRUD (create, read, update, delete) operations of relational databases and to the four fundamental HTTP request methods: POST, GET, PATCH, and DELETE.
Explain a RESTful style of development in Rails
The RESTful style of development helps you make choices about which controllers and actions to write: you simply structure the application using resources that get created, read, updated, and deleted.
A model file in apps/models generated by scaffolding appears with little to no content; how does it get its functionality?
Via inheritance from ActiveRecord::Base