Friday, May 29, 2009

Adding Times Ago

We will have to make use of the timestamp created_at data that was automatically assigned when the data is created.

In the view of html.erb.
<%= time_ago_in_words(m.created_at) %> ago

Customize Root

To display your own index page in http://localhost:3xxx.

In config/routes.rb for instance,
map.root :controller => 'posts'

And be sure to delete the index page in public/index.html

Monday, May 25, 2009

Clean URL Namings

Make sure the application applies the RESTful method

route.rb
ActionController::Routing::Routes.draw do |map|

map.resources :posts

end

apps/model/Post.rb
class Post < ActiveRecord::Base
validates_presence_of :name, :crews

def to_param
"#{id}-#{name.gsub(/\s/,'_').downcase.gsub(/\?/,'')}"}"
end

end

"#{id}-#{name.gsub(/<Regular Expressions>,'<Replace using gsub>').downcase}"

http://e-texteditor.com/blog/2007/regular_expressions_tutorial for more information regarding Regular Expressions.

It is crucial to deal with the punctuation to avoid routing errors.

In the View
<%= link_to post.title, post_path(post) %> // With Routing

<%= link_to post.title, :action => 'show', :id => 'post' %> // Without Routing

I referred to

http://railscasts.com/episodes/63-model-name-in-url

http://blog.imperialdune.com/2007/2/13/pretty-urls-with-restful-rails

http://www.matthias-georgi.de/2007/4/pretty-restful-urls-in-rails