Here below are some notes I made while playing with Ruby and RoR. Interestingly, I found that RoR is a great way to learn about the foundations of web frameworks. I have a much stronger understanding about not just RoR, but of Django, Pyramid, and Web2Py. Every web framework really has the same few components (or some variation thereof): Models, Views, Controllers, and Routes.
I walked through the free tutorial by CodeSchool:
The Basics:
CRUD
Create,Read,Update,Destroy
Zombie.new
z = Zombie.new(initialize)
z.save
Zombie.create
Zombie.find(3)
Zombie.update_attributes
Models:
class Tweet < ActiveRecord::Base (just means that class Tweet inherits from ActiveRecord)
end
validate data before it is saved in DB
class Tweet < ActiveRecord::Base
validates_presence_of :status
end
t.errors -> returns errors
t.errors[:status] --> just error pertaining to status
Rails 3 new syntax for validation:
validates attribute, validation
validates :status, :presence => true
validates :status, :length => {:minimum => 3}
app/models/tweet.rb
class Tweet < ActiveRecord::Base
belongs_to :zombie
end
app/models/zombie.rb
class Zombie < ActiveRecord::Base
has_many :tweets
end
View:
web request -> 4 layers -> models, view, controllers, routing
<%...%> evaluate ruby
<%=...%> evaluate and print results
layouts/application --> make html page format and use <%= yield %>
/app/views/tweets/shot.html.erb (code that will be yielded is in here)
Adding CSS
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %> # can replace prototype javascript library with jquery
<%= csrf_meta_tag %> #protects website from hackers
with URLs, it checks public folder first, then tries to execute inside rails
Adding a Link
<%= link_to "link text", "link path (URL)"%>
<%= link_to tweet.zombie.name, zombie_path(zombie.tweet) %>
<%= link_to "Edit", edit_tweet_path(tweet) %>
<%= link_to "Delete", :method => :delete %>
Listing Zombies
Listing Tweets
<% Tweet.all.each do |tweet| %>
<% end %>
| Status | Zombie |
| <% tweet.status %> | <% tweet.zombie.name %> |
Controllers:
class TweetsController < ApplicationController
def show
@tweet = Tweet.find(params[:id]) #using an instance variable
render :action => 'status' #for status.html.erb
respond_to do |format|
format.html #show html.erb
format.xml { render :xml => @tweet }
format.json { render :json => @tweet }
end
end
model calls goes into the controller files. All request data stored in a hash called params.
def index -> list all tweets
def show -> show a single tweet
def new -> show a new tweet form
def edit -> show an edit tweet form
def create -> create a new tweet
def update -> update a tweet
def delete -> delete a tweet
Authorization:
def edit
@tweet = Tweet.find(params[:id])
if session[:zombie_id] != @tweet.zombie_id
flash[:notice] = "sorry, you can't edit this tweet"
redirect_to(tweets_path)
end
end
end
class TweetsController < ApplicationController
before_filter :get_tweet, :only => [:edit, :update, :destroy]
before_filter :check_auth, :only => [:edit, :update, :destroy]
def get_tweet
@tweet = Tweet.find(params[:id])
end
def check_auth
if session[:zombie_id] != @tweet.zombie_id
flash[:notice] = "sorry, you can't edit this tweet"
redirect_to(tweets_path)
end
end
Routing:
config/routes.rb
ZombieTwitter::Application.routes.draw do |map|
resources :tweets #creates a "REST"ful resource
match 'new_tweet' => "Tweets#new" # path => controller#action
match 'all' => "Tweets#index", :as => "all_tweets" # now all tweets can be used as an object "link_to"
match 'a' => redirect('/tweets')
match 'google' => redirect('http://www.google.com')
root :to => "Tweets#index"
match 'local_tweets/:zipcode' => "Tweets#index"
match 'local_tweets/:zipcode' => 'Tweets#index', :as => 'local_tweets'
end
<%= link_to "All Tweets", all_tweets_path %>
<%= link_to "Tweets in 32828", local_tweets_path(32828) %>
0 comments:
Post a Comment