I wanted to add an index and about page to my site but avoid repeating the overall page design stored in app/views/layouts/application.html.erb. I found the basic idea on Robert Evans' site. The application.html.erb file is what the rails routing machine falls back on as a default template if it doesn't find one of the same name as the controller.
- Create a new controller to manage the pages. At the control line (positioned within your application folder) enter:
script/generate controller static index about
The script/generate contoller bit tells rails to create the controller file (app/controllers/static_controller.rb) and the associated views folder (app/views/static/) . The next argument (static) is the name of the controller to be created. The final arguments (index, about) are optional and tell rails to create methods of those names in the controller file and the appropriate files in the views folder (eg. app/views/static/index.html.erb). - Edit the view files to contain the required content
- You can customise parts of application.erb.html for example create a custom title by changing the title line in the header to
<title>YourSiteName <%= @page_title if @page_title %></title>
and modifying the methods in the controller file todef index @page_title = "Home" end - Add the following to the "config/routes.rb" file
map.root :controller => 'static', :action => 'index'
- Refer to the pages using
link_to :controller => 'static', :action => 'index' link_to :controller => 'static, :action => 'about'
No comments:
Post a Comment