Hack hack hack...

An open journal-- some of it written for you, but most of it is for me.

Lecture 2 Rails

Lecture 2

BASH

1
2
3
4
cd .. # change to up a directory
cd . # change to current directory
ls # list
cd ~ #home directory

Resources - the thing you want to let users manipulate

1
rails generate resource Mixtape nae:string

Rake- a ruby language for running scripts

Models- main component of your application, they represent the resources of your application every model is just a ruby class definition located in the model

so the mixtape model defines a class Mixtape that is the object that will allow our app to CRUD mixtapes

If we a Users model we have a users table

Goal for today is to understand the request cycle

Routes

DSL- Domain Specific Language (3:08PM)

1
2
match '/foo' => 'songs#foo' # this needs to be sent to a controller
# a songs controller with the foo action

we need to find something that can handle the route–> we want to send it to a waiter, like ordering an A7 at a chinese resturant

an ACTION is just a method defined in the class!! a Controller is just a class –> a grouping that relates to songs

if it can’t find a page it is 404

every action in the app needs to be defined

5 keywords available… MATCH GET POST PUT DELETE

cmd T –> opens a window for you to access files

rake routes: first column is the request second is http verb third is url or pattern fourth is the contoller#action

resources macro- defines all the restful actions for the resource

?? so what was the main problem with the post request?

Controllers & #actions

e.g. of actions in the Songs controller
1
2
3
4
5
6
7
def name
  render :template => 'songs/name' #explicit template rendering
end

# exactly the same as this...
def name
end
Renders
1
2
3
4
render :action => "name" # in the views folder named after the controller
render :template => 'songs/name' # need to specify the full path
render :xml
render :json

Video 2 starts 4PM diff between <% and =<% <% will get executed, but will not show in the html <%= will get executed and will also show in the html

Instance variables Instance variables are the only thing passed from the controller to the views ??? do class variables get passed as well?

link_to

1
<%= link_to 'Search', 'http://google.com' %>

we have two methods path and url

e.g. of link_to
1
<%= link_to 'Show', 'song' %> #polymorphic interface 

3 pars of rails (4:10 PM) ORM- active record Action controller Action view

LEARN MORE: there are lots of polymorphic interfaces play nicely with active record

automatic routing with namespacing with_options
1
2
3
4
with_options :controller => 'site' do |s|
  s.get 'about'
  s.get 'home'
end

Comments