I'm trying to use this technique (http://blog.hasmanythrough.com/2008/4/2/simple-pages) on "boiler-plate" pages I want to keep in a subdirectory of my controller's view folder.
How would I do this? The below doesn't seem to work: returns a "Template missing" error.
Error:
Missing template home/New_York_apartments.erb in view path app/views
/app
/controllers
home_controller.rb
/old_pages
home_controller.rb
/views
/home
about.html.haml
contact.html.haml
index.html.haml
/old_pages
New_York_apartments.html.haml
routes.rb
map.namespace :old_pages do
map.connect ':page', :controller => 'home', :action => 'show', :page => HomeController::PAGES
end
map.home ':page', :controller => 'home', :action => 'show', :page => HomeController::PAGES
controllers/home_controller.rb
class HomeController < ApplicationController
# boiler-plate pages
PA开发者_StackOverflow社区GES = ['about','contact']
def index
# homepage
end
def show
render :action => params[:page] # passed in our routes
end
end
controllers/old_pages/home_controller.rb
class OldPages::HomeController < ApplicationController
# boiler-plate pages
PAGES = [
'New_York_apartments' # apprently something to do with new york apartments; who knows
]
def show
render :action => params[:page] # passed in our routes
end
end
Here is the source of your problem:
map.namespace :old_pages do
map.connect ':page', :controller => 'home', :action => 'show', :page => HomeController::PAGES
end
This tells Rails to prefix oldpages/
to any path it needs to look up. The controller will be at app/controllers/old_pages/home_controller
and respond to urls that start with /old_pages/home
. And it's expecting the views to be in app/views/old_pages/home/
. Your directory layout places views at app/views/old_pages/home
No matter how you choose to solve this problem you're going to be moving files around, and possibly editing others.
Least Effort Solution: Rename your current app/views/home/old_pages
directory to app/views/old_pages/home
. If you're migrating your entire site and want to keep the current version around for stubborn users this is the best way to do it.
Alright, found a solution. I traded a slightly more bloated main home_controller.rb
for a cleaner controllers directory and routes file, which considering these pages will eventually be eliminated (the ones in the subdirectory), I thought was a fair compromise.
Edited files reproduced below:
/app
/controllers
home_controller.rb
/views
/home
about.html.haml
contact.html.haml
index.html.haml
/old_pages
New_York_apartments.html.haml
routes.rb:
# custom routes
map.root :controller => 'home', :action => 'index' # root page
map.home ':page', :controller => 'home', :action => 'show', :page => HomeController::PAGES # boiler-plate pages
map.connect ':page.html', :controller => 'home', :action => 'show_old_pages', :page => HomeController::OLD_PAGES # old static pages; only here to make the transition
controllers/home_controller.rb:
class HomeController < ApplicationController
def index
# homepage
end
def show
render :action => params[:page] # passed in our routes
end
def show_old_pages
render :action => "old_pages/#{params[:page]}"
end
#### boiler-plate pages ####
PAGES = [
'about',
'contact'
]
OLD_PAGES = [
'New_York_apartments' # apprently something to do with new york apartments; who knows
]
end
精彩评论