Help creating a route and controller for static pages
I have some static pages I am trying 开发者_高级运维to make like team, aboutUs, terms of service, etc.
I am trying to make one controller to handle the static pages, but I am missing some know-how.
Here is what I did:
In routes.rb, before the end I added this:
match "/:action" => "pages"
Then I made a controller named pages_controller.rb
Currently it is empty. What I need it to do is recognize the requests like /pages/team or pages/about_us and redirect to the right static page. How can I do that?
Thank you!
This is how I do it:
match '/pages/:page' => "pages#page"
Then based on params[:page]
i render different static views.
This works good for me, for sites with a smaller number of static pages.
Of course you can explicitly name your routes:
match '/about-us' => "pages#about_us"
and then declare an empty method for each route in your Pages controller:
def about_us
end
but I prefer the first way.
Another possible way to do this is to put something like this at the bottom of your routes:
map.connect '*path', :controller => 'your single controller name', :action => 'show'
*path basically says to pick up anything not already covered in route declarations above.
I'm pretty sure :action => 'show' indicates to skip the controller altogether (hence no need for an empty controller method) and just renders the template with the same name in the same path as your controller.
I've even gotten it to work with subdirs...
app/views/your single controller name/some-new-subdir/foo.html.erb for instance would find a match for the route:
http://example.com/some-new-subdir/foo
精彩评论