Reputation:
Quite new to Ruby on Rails and I am stuck on probably an easy task. Basically I am working on a colleague's app and need to add an additional page that shows users how the app works. I have already written the HTML and styles. I just don't know how to exactly add it to Rails and configure the routes properly. Any help would be appreciated!
Upvotes: 32
Views: 44276
Reputation: 20645
First make sure that your colleague did not already create a controller to handle static pages. Look under app/controllers
for controllers titled anything similar to directories_controller
or pages_controller
, etc. If he/she has, follow the pattern your colleague already set up (you might want to ask him/her for guidance at that point). If there is not static pages controller like that, then follow the advice below.
You can create a controller named something like PagesController
that defines methods that match a route. For instance, your additional page might be called "help", in which case you can define a controller like so:
class PagesController < ActionController::Base
def help
# put any code here that you need
# (although for a static view you probably won't have any)
end
end
Then you'll want to create a new folder under app/views
titled pages
, and you can add your static page there (app/views/pages) with a .erb
extension. Using a .erb
will allow your new page to use the default layout.
Finally, you'll need to add this controller to routes.rb
in (config/routes.rb)
to tell rails where to look for the /help
page:
match '/help' => 'pages#help'
Upvotes: 43
Reputation: 14973
If your pages are truly static(nothing dynamic in them), you can drop them in the /public
directory, and they will be directly accessible.
A file in ../public/help.html
will be accessible at http://yourdomain.com/help.html
If you want to use your layouts but just provide static content, you can make a controller, routes and views for that as follows.
# static_controller.rb
class StaticController < ApplicationController
def show
render params[:page]
end
end
# towards the end of routes.rb
get "/:page" => "static#show"
And make your views in app/views/static
. You can use just plain html or erb as well. A view called help.html.erb
will be available at http://yourdomain.com/help
Any view file you create under app/views/static
will be available without modifying your routes or controller.
Upvotes: 19