Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a new page in Ruby on Rails

People also ask

How do you create a page in Ruby?

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.


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'

Static pages

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

Semi static pages

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.