Chapter 2 Flashcards
create new rails app(all of its directories generated)
cd workspace
rails 4.2.2 new toy_app
cd toy_app
$ bundle install –without production
flag prevents local installation of any production gems
what must you do after you made changes to the gem file
run bundle install
put rails app under version control
git init
git add -A
git commit -m “Initialize repository”
pushing rails app after putting app under version control:
git remote add origin https://github.com/saweel/toy_app.git
git push -u origin master
(must create repository manually in github and then push it into it (thats what she said))
typical first step when making a web application:
create data model, represenation of the structures needed by out application. in toy app, microblog, with only users and short posts. model for users and then model for microposts
data model for a user
interger identifier (id), name (string), email (string)
model for micropost
id (integer) and content text (string), we want to associate each post with a user, so we need user_id (integer) for the post
user resource:
allow us to think users as objects that can be created, read, updated, and deleted through the web via the HTTP protocol. generage it using scaffold
use scaffold command to generate a resource (user) with parameters for attributed: name and email of type string. why dont you need to include id?
$ rails generate scaffold User name:string email:string. no need to include parameter for id; it is created automatically by rails for use as the primary key in the database
command to migrate database using Rake
$ bundle exec rake db:migrate. simply update the database with our new user data model. bundle exec to make sure rake corresponds to our gemfile version.
rails s
shorcut for rails server
make utility
in unix, building executable programs from source codes. commonly used to compile code
rake
rails uses rake extensively especially in developing database-backed web applications. rake db:migrate most common.
(root URL)/users
page to list all users (index)
/users/1
page to show user with id 1 (show)
/users/new
page to make a new user (new)
/users/1/edit
page to edit user with id 1 (edit)
heroku create
create subdomain name for us.
why is show and update action correspond to the same url
difference is the http request method they respond to.
what is REST?
architectural style for developing distributed, networked systems and software applications such as web applications.
how does REST model users and microposts?
application components are modeled as resources that can be created, read, updated, and deleted - operations both to the CRUD of relational database and to HTTP request methods:
HTTP request methods:
POST, GET, PATCH DELETE (learn more later on)
@users = User.all
list all users from User object in model database and place them in variable
mvc model summary to go to /users: (long answer 8 steps)
- brower issues request for /users URL
- Rails routes /users to index action in Users controller
- index action asks User model to get all users
- User model pulls users from database
- User returns list of users to controller
- stores in @users variable, which is passed to the index view.
- the view uses embedded Ruby to render the page as HTML
- controller passes the HTML back to the browser
mvc model of /users in more detail (long answer)
- request issued from browser (typing the url).
- sent to rails router which determines the controller action based on URL.
- action has @users = User.all
- which asks User model to get all users.
- stores users in @users.
- controller calls the html.erb view and iterates through @users and otputs HTML code
- view converts to HTML.
- returned by the controller to the browser for display.
how URL to route knows which action to perform?
in config/routes.rb, we create controller action pair with URL. example: root ‘users#index’.
weaknesses of User resource
- no data validations(email can be wrong)
- no authentication (cannot login or logout and no way to prevent user from perfoaming any operation)
- no style or layout
- no real understanding
- no static pages (such as home or about)
- no user passwords
- no user images
- no security
- no following or followed, and more
generate scaffold for Micropost
$ rails generate scaffold Micropost content: text user_id:integer
after generating a new resource using scaffold, what do we have to do?
must run bundle exec rake db:migrate to update the database
resources :microposts in route -
maps micropost URLs to actions in the Micropost controller
constraint micropost text field to 140 characters(same as twitter)
app/models/micropost.rb
add:
validates :content, length: { maximum: 140 }
write code to show a user has many microposts
has_many :microposts
app/models/user.rb
code to show a micropost belongs to a user
app/models/micropost.rb
belongs_to :user
$ rails console
useful tool for interacting with rails applications
first_user = User.first (c)
store first user in variable
first_user.microposts (c)
see all posts of user
micropost = first_user.microposts.first
see first post of user
micropost.user
see user info
what do we gain from inheriting from ActiveRecord::Base
model objects gain the ability to communicate with the database, treat the database columns as Ruby attributes, and so on.
user and micropost controller inheritance tree:
UserController
inheriting from ActionController::Base provides what abilties?
manipulate model objects, filter inbound HTTP requests, and render views as HTML.
to get heroku database to work:
$ heroku run rake db:migrate
strength of this chapter:
high level overview of rails
- intro to MVC
- first taste of REST architecture
- beginning data modeling
- a live, database backed web application in production
what is scaffolding
automatically creates code to model data and interact with it through the web