ROR 4 essential training Flashcards
what is rails
opensource web app framework
what is dry (fundamental to rails)
dont repeat yourself
provides consise consistant code
convention over configuration (second fundamental of rails)
sensible defaults, only specify unconventional aspects
this increases speed bc we have default code to rely on and less code to maintain
model
data objects (like objects in database)
view
basically what you see (html, css, etc)
controller
makes decisions and controls aoo based on interaction
what does rails call the controller
ActionController
What does rails call the view
ActionView
what does rails call the model
ActiveRecord
what is ActionPack
the controller and view packaged together as one thing (ActionController and ActionView)
update rubygems
gem update –system
bundler gem helps how
installs all the gems for a particular rails aplication/environment (different versions for different applications, etc)
list installed gems
gem –list
install a gem (and flags to not install ri or rdoc, and specify version)
gem install x –no-ri –no-rdoc –version 4.0.0
what web servers does rails run by default
webrick
for deployment rails should be hosted on another server such as
apache,nginx
how to create rails project
rails new projec_name -d server_type
where is the root of the directory of the project
project_name/
gemfile contains what
gems we want to load, and their version/source
gemfile.lock contains what
all the gems, and what those gems depend on
how to install new gems
add to gem file then run bundle install
how to start rails server
rails s
default port for webrick
3000
database.yml
located in the config folder, contains configuration for connecting to the database
On the first starting of the server what should be modified in database.yml and what else needs to be run to get everything to run succesfully
edit username to what you created
run:
rake:db:create:all
rake db:migrate
a minimum rails app can run on what three components
C,V,browser
how to create a controller with view
rails generate controller cname vname
when generating a controller with a second paramater as a view what happens
creates cname_controller.rb in app/controllers
creates index.html.erb in app/views/demo/
creates route get “demo/index” (which is exactly what is put in the route file– also by default if you want to access this page thats what you have to append to the site name)
a generated controller has what in it by default if generated by controller and view name of index
class cnameController < ApplicationController
def index
end
end
the < denotes that cnameController inherits from ApplicationController
the def index is a simple action that, even though it has nothing in it, it renders a view and uses a layout
how to disable a layout from the controller
layout false
by default a rails view contains what
some basic html
app directory contains what
assets, mvc, helpers, and mailers
helpers folder contains what
most of the ruby code to support the view
mailers folder is for what
sending email
assets folder is for what
images/stylesheets/js
bin folder contains what
bundle, rails, rake
config folder contains
configuration for app, be it database.yml, application, or the subfolder environments for (test, production, and development)
initializers folder
things we need to launch when app launches, like config files
config.ru is what
the config file for rack that works with rails
lib folder
assets, tasks>contains tasks that we might write
concern about public folder
anything in public folder is visible to public (alternate place for images/js/stylesheets)
test folder
hold the code for TDD like apps
tmp folder
cookies, sessions, etc
vendor folder
third party code
when a web server requests something, what can intercept it from ever reaching the rails architecture?
if its directly in the public folder
what does routing do
parses url for which controller and action to user
three route types
simple, default root
how does rails routes handle a get request
GET “page/subpage”by processing PageController#page as html and then renders template
rails knows how to do this via the route file after generating the controller command
simple route
get “demo/index”
match route (basically the steps in how route works)
get “demo/index”
match:”demo/index”,
: to =>”demo#index”,
:via => get
if route doesnt find a match what happens
rails returns routing error
default route structure
:controller/:action/:id
ex GET /students/edit/52
says go to StudentsController, end perform edit action on 52
default route example
match ‘:controller(/:action(/:id))’, :via => :get
very important to add
default route allows the absence of what
an action, it applies a default using match
default route that returns a type of format like json
just like default with additional format
match ‘:controller(/:action(/:id(/.:format)))’, :via => :get
root route
when you go to the root of the application and have nothing to match, this tells it where it should go
root “demo#index”
should be placed at end of routes file due to it processing top to bottom
route processed in what order
top to bottom
in a controller for an action like (def index end) what is the default behavior
load index template in demo directory
knows template should be index bc thats the name of the action, and knows its in demo dir because that s the name of the controller
is an action required for each template
no, but it is good practice (i.e. def index end)
how to specify a template for an action
render(:template => ‘demo/hello’)
short hand if youre in the same directory you can narrow to just a string i.e. (render(‘hello’)
what is a redirect
sends request to different controller and action
redirect keyword
redirect_to(:controller => ‘demo’, :action =>’index’)
if redirecting to within same controller, what can be left out in the redirect function
the controller
does a redirect need to render a layout
false
can you use redirect_to to redirect to external site?
yes, just use url in string
how do we embed ruby code into our site
ERb (embedded ruby)
different parts of hello.html.erb
hello:template
process with:erb
output format:html (you could use alts like xml etc)
how to embed code in erb
’% code %’
also ‘%= code %’
difference between
‘% code %’
also ‘%= code %’
first executes (any attempted output may be shown in command prompt), second executes then outputs
execute a block of code x times
‘%x.times do%’
(if you want to out put use:)’%= code %’
‘%end%’
in short you can have a code flow broken up and only have the output in the = part
a way to pass data from controller to view
instance variables (a variable applies inside this instance of an object, in rails, the controller is the object)
is a controller a class?
yes view
are non instance variables in the controller available to the
no
link method
’%= link_to(text, url) %’
for url you can use regular “/demo/index/”
or you can specifically pass a hash {:controller => ‘demo’, :action => ‘index} (same applies here, where if its in the same demo, you can just pass action)
add url paramaters to link (those that start after ? and seperated by &)
{ :controller=>'demo', \:action=>'hello', \:id=>1, \:page=>3, \:name=>kevin} as always, can leave out controller and action if theres a default
constructs
/demo/hello/1?page=3&name=kevin
shorthand for hash if all keys ar symbols
hash={font_size:10} vs hash={fontsize =>10}
HashWithIndifferenntAccess
allows you to access hash keys using symbol or key
params
returns hash with all paramaters sent in get and post hash
params is accesible where
view and controller, but should be accessed in controller
assign params to an instance var
@page= params[:page]