krisleech wrote:
Hi, I’m designing a simple CMS, and hope to share the code.
Objectives of CMS:
- Simple scaffold backend with password protection
- View a page either by specifying the id or title
- View a list of pages
- View latest 5 pages in a section
- View all pages in last month
- View all pages in a section paginated
- URL looks like static site, /folder/page
- Homepage displayed if no page specified
- Same template for all pages
- Handle page not found
So my idea is:
Authors (has_many) Pages (belongs_to) Section
Page
index
view
view_by_id
view_by_title
view_all_in_section
view_latest_in_section
The actions that return one page (@page) will share a view and the
actions that return more than one page (@pages) will share a view.
Now this is where my questions come in:
- Do the action names looks good, any better suggestions?
- Can I use routes to prevent my action names showing in the URL eg.
“domain.com/page/view_by_id/3” or
“domain.com/page/view_all_in_section/5”
Instead I want these URLs:
domain.com/section/page
Default title | Domain.com
domain.com/section/archive
domain.com/section/latest
Something doesn’t quite seem right, because I want my URL to include the
section, but the actions dont need the section as input, only the page
id or title.
Do I need to re-think, should I always include the section, and if no
section is given then use section_id = 1 (root section).
Does the structure of my controller / actions / URL’s seem correct?
You could use something like this :
map.connect ‘:section/:page’, :controller => ‘page’, :action => ‘show’
map.connect ‘:page’, :controller => ‘page’, :action => ‘show’
map.connect ‘:section/archive’, :controller => ‘page’, :action =>
‘archive’
map.connect ‘:section/latest’, :controller => ‘page’, :action =>
‘latest’
map.connect ‘:controller/:action/:id’
You can even use routes.rb to check if a param i s a number or not and
then send to a different action depending on the result.
Ex /page/78 send to action show_by_id
/page/about send to action show_by_title
Have a look in the agile book. There is a good example in there.
I use the method here under to create an uniq_title_id (based on the
page title) that is url compatible for each of my pages in my page model
in a before_create filter (this app is in french)
ex (in french) : Page about “ruby on rails” will have a uniq_txt_id :
page_about_ruby_on_rails
def create_uniq_txt_id
foo = self.title.downcase.strip
foo.gsub!(/[Ã?ÃÃ?Ã?âäà ãáäåÄÄ?Ä?Ç?Ç?Ç¡Ç»ÈÈ?ȧẵặ]/,‘a’)
foo.gsub!(/[ëêéèẽÄ?Ä?Ä?ẻÈ?È?ẹȩÄ?á¸?á¸?á»áº¿á»?á»?á¸?á¸?á»?á¸]/,‘e’)
foo.gsub!(/[Ã?ÃÃ?ĨÃiìÃîĩīÄïá»?Çá»?įÈ?È?á¸É¨á¸¯]/,‘i’)
foo.gsub!(/[Ã?Ã?Ã?Ã?Ã?òóôõÅÅȯöá»Å?Ç?ÈÈÆ¡Ç«á»ÉµÃ¸á»?á»?á»?á»?ȱȫÈá¹á¹á¹?á¹?á»á»?ỡá»?ợÇá»?Ç¿]/,‘o’)
foo.gsub!(/[Ã?Ã?Ã?ŨÃ?ùúûũūÅüủůűÇ?È?È?ưụṳųṷṵṹṻÇ?Ç?Ç?Ç?Ç?ừứữá»á»±]/,‘u’)
foo.gsub!(/[ỳýŷỹȳáºÃ¿á»·áº?ƴỵ]/,‘y’)
foo.gsub!(/[Å?]/,‘oe’)
foo.gsub!(/[Ã?ǼǢæ]/,‘ae’)
foo.gsub!(/[ñǹÅ?]/,‘n’)
foo.gsub!(/[Ã?ç]/,‘c’)
foo.gsub!(/[Ã?]/,‘b’)
foo.gsub!(/[Å?]/,‘oe’)
foo.gsub!(/[ij]/,‘ij’)
foo.gsub!(/[\s'"\/?.=+&%]/,‘')
foo.gsub!(/+/,’')
# test if exist
bar = Scoop.find_by_uniq_txt_id(foo)
foo = foo + '’ + self.user.login unless bar.nil?
self.uniq_txt_id = foo
end