> rails new blogpress
creates new directory contains:
– Gemfile – home to GEM dependencies
– app – where your models, views, and controllers
– config – this is where your configurations; routes, etc.
– db – database schema and migrations (SQLLite)
– log – development, test, and production logs
– public – web accessible directory, image, stylesheets, javascript, etc.
– Vendor – centralized location for 3rd party code from plugins to gem
> bundle install
bundler is a tool that installs required gems for your application. Bundler is able to determine dependencies and children without instructions from you
database configuration (database.yml)
defaults to sqlite, allows for mysql and postgreSQL
> rake db:create
– reads the datbase.yml file and creates db
rake, WTF?
Rake is a way to run tasks
rake -T = shows all tasks you can do
> rails server
http://127.0.0.1:3000/
> rm public/index.html
We don’t want this page displaying when visitors come to our blog
> rails generate controller home index
Generate a controller with only an index action to take the pklace of public/index.html
Update config/routes.rb to let the app know that our root should be
REMOVE get “home/index”
ADD root :to => “home#index”
Any rails file ends in .rb
Any rails view file is .erb
EVERY BLOG HAS POSTS
> rails generate scaffold Post author:string title:string content:text permalink:text
This file does ALL the work for you.
strings have a default length of 256 character
text has a default length of 1024 character (?)
float, decemial also other types
Scaffold creates full CRUD functionality for us, including our model, view, controller, and even our database mirgration
> rake db:migrate
Calls our self.up method to create the posts database table
http://127.0.0.1:3000/posts
Should be seeing “Listing posts”
> rails console
You can test pretty much anything right here.
Post.all
Post.all.first
Post.all.first.title
(Use capital ‘P’ for the Posts object, not a particular post)
– You can add a .xml to the end to automatically spit out xml (rails by default spits out HTML)
http://127.0.0.1:3000/posts.xml
– In your posts_controller.rb, add this for example to spit out JSON:
format.json { render :json => @posts }
Blog Comments:
> rails generate scaffold Comment post:references email:String content:text
> rake db:migrate
Replace
resources; comments
resources: posts
with
resources :posts do
resources :comments
end
/posts – /comments
/posts/1 – /comments/1
/posts/1/edit – /comments/1/edit
Changes now made urls like this:
/posts/1/comments/
/posts/1/comments/1
/posts/1/comments/1/edit
@post = Post.find(params[:post_id])
@comments = @post.comments
http://pastie.org/1307643
http://pastie.org/1307632
BLOG: TAGS/KEYWORDS
We are going to use a Gem here
Add this to your Gemfile, at the end of your file (in your root):
gem ‘acts-as-taggable-on’
> bundle install
(might have to restart rails console before typing in the below:)
> rails generate acts_as_taggable_on:migration
> rake db:migrate
acts_as_taggable
http://pastie.org/1307777
Don’t forget to update your show.html.erb to include:
Tag:
<%= @post.tags %>
BLOG: Validation Options
:presence => true
:uniqueness => true
:numbericality => true
:length => { :minimum => 0, maximum => 2000 }
:format => { :with => /.*/ }
:inclusion => { :in => [1,2,3] }
:exclusion => { :in => [1,2,3] }
:acceptance = > true (deals with checkboxes)
:confirmation => true (deals with checkboxes)
post.rb:
validates :author, :presence => true, :length => {:maximum => 30 }
validates :title, :presence => true, :length => {:maximum => 100 }
validates :content, :presence => true
comment.rb:
validates :email, :presence => true
validates :content, :presence => true
Blog: Validation
Basic HTTP authentications prompts users via a browser popup. This authentication lasts until the browser is closed.
http://pastie.org/1307757
Blog: Most Recent Posts
We want to display the top n posts on the main page, of course we’ll want the n more recent posts
http://pastie.org/1309253