Posted By

nate63179 on 11/12/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

smoothdzion
octoberborn


Static pages controller


 / Published in: Rails
 

  1. #
  2. # PagesController
  3. #
  4. # This controller works differently than most normal rails controllers. The pages controller here is used
  5. # as a traffic cop for static and semi-static pages in the rails site, with database information collected
  6. # from the necessary classes.
  7. class PagesController < PublicController
  8. layout 'public'
  9. caches_page :show, :index
  10.  
  11. # Renders the home page for the site
  12. def index
  13. @page = Page.find_by_name("home")
  14. end
  15.  
  16. # Attempts to render a page directly from the views/pages directory. If no page is found a 404 will be rendered
  17. def show
  18. action_name = params[:path].join('/')
  19. @page = Page.find_by_path("/" + action_name)
  20.  
  21. # Check to see that the page exists in the database and has a corresponding template file
  22. if @page && page_exist?(action_name)
  23. load_restaurant_page_data if @page.in_dining?
  24. render :action => action_name, :layout => @page.view_layout
  25. else
  26. render :action => "404", :layout => true, :status => 404
  27. end
  28. end
  29.  
  30. private
  31.  
  32. # Deterine if the page we're trying to access exists
  33. # <tt>path</tt> The name of the file we're trying to access, like "visit-telluride/index"
  34. def page_exist?(path)
  35. file_path = File.join(RAILS_ROOT, "app", "views", "pages", "#{path}.html.erb")
  36. return true if File.exists?(file_path)
  37. end
  38.  
  39.  
  40. # Returns a layout for special case pages. Currently, all views except the app/views/photos/index use the public layout
  41. def find_layout(page_name)
  42. action_name == ""
  43. end
  44.  
  45. end

Report this snippet  

You need to login to post a comment.