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
0 comments:
Post a Comment