Ruby on Rails: Rails 3 (part 2)

To create a link:

<%= link_to "Some place", "http://www.anylink.com" %>
 
#Below are links taken from rails
<%= link_to 'Edit', edit_post_path(@post) %>
<%= link_to 'Back', posts_path %>
<%= link_to 'Show', post %></td>
<%= link_to 'Edit', edit_post_path(pos2t) %>
<%= link_to 'New Post', new_post_path %>
<%= link_to 'Show', @post %>
<%= link_to 'Back', posts_path %>
<%= link_to 'Back', posts_path %>
<%= link_to "My blog", posts_path %>
<%= link_to 'Destroy Comment', [comment.post, comment], :confirm => 'Are you sure?', :method => :delete %>

Other interesting functions from Rails (scaffolding):

<%= stylesheet_link_tag    "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= @post.name %>
<%= @post.title %>
<%= @post.content %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
<%= render 'form' %> # (app/app/views/posts/edit.html.erb)
<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>
<%= form_for(@post) do |f| %> # (app/app/views/posts/_form.html.erb)

Models, Rails Console and ActiveRecords

validation of model in model class (e.g. app/models/post.rb)

class Post < ActiveRecord::Base...
  validates :item1, :presence => true, [other...]
  validates :title, :presence => true, :length { :minumum => 5 }

This can be easily tested with rails console
( http://guides.rubyonrails.org/getting_started.html ); console gives full access
to objects in Scaffolding:

>>Post
 => Post(id: integer, name: string, title: string, content: text, created_at: datetime, updated_at: datetime) 
>>Post.methods # lists all methods accessible in class
 
 
>>a = Post.new(:content => "abc", :title => "some title here", :name => "vito")
 => #<Post id: nil, name: "vito", title: "some title here", content: "abc", created_at: nil, updated_at: nil> 
>> a.save
  SQL (42.0ms)  INSERT INTO "posts" ("content", "created_at", "name", "title", "updated_at") VALUES (?, ?, ?, ?, ?)  [["content", "abc"], ["created_at", Sat, 05 Nov 2011 09:41:15 UTC +00:00], ["name", "vito"], ["title", "some title here"], ["updated_at", Sat, 05 Nov 2011 09:41:15 UTC +00:00]]
 => true 

If validation is not passed - a.save in above returns false. This indicates an error
that can be read back using p.errors

Following selects all Posts from database; below function is used by controller (app/controllers/posts_controller.rb):

Post.all
  Post Load (0.9ms)  SELECT "posts".* FROM "posts" 
 => [#<Post id: 1, name: "Abc", title: "Def", content: "ASd", created_at: "2011-11-04 16:55:37", updated_at: "2011-11-04 16:55:37">, #<Post id: 2, name: "aSdasds aaaAAAAA", title: "ASDFA", content: "adfadf", created_at: "2011-11-04 16:56:15", updated_at: "2011-11-04 16:56:15">, #<Post id: 3, name: nil, title: nil, content: "A new post", created_at: "2011-11-05 09:31:41", updated_at: "2011-11-05 09:31:41">, #<Post id: 4, name: "vito", title: "some title here", content: "abc", created_at: "2011-11-05 09:41:15", updated_at: "2011-11-05 09:41:15">]

ActiveRecord::Base

Post.all is inherited from ActiveRecord::Base (inheritance happens in above
file app/models/post.rb ). More information about ActiveRecord:

Whole application!

Not was not as bad as I initially expected...

We can see:
ActionController::Base inherited via application_cntroller.rb into posts_controller. It is core of rails app and more about it here: http://api.rubyonrails.org/classes/ActionController/Base.html.
ActiveRecord::Base inherited into model Post.