RailsApps Pages Gem
Use this gem to add pages to a Rails application, including controllers, views, routing, and tests.
RailsApps Pages is a utility gem to use during development. You can remove it after generating the pages you need. It was originally written for use by the Rails Composer tool. Use Rails Composer to build any of the RailApps example applications for use as starter apps.
If you like the RailsApps Pages gem, you might be interested in the RailsLayout gem which generates Rails application layout files for various front-end frameworks such as Bootstrap and Foundation.
Support the RailsApps Project
If the RailsApps Pages gem is useful to you, please accept our invitation to join the RailsApps project. The RailsApps project provides example applications, tutorials, and starter tools, including the RailsApps Pages gem.
Install the Gem
Add the gem to your Rails application Gemfile:
group :development do
gem 'rails_apps_pages'
end
You don’t need the RailsApps Pages gem deployed to production, so put it in the development
group.
If you want to use a newer unreleased version from GitHub:
group :development do
gem 'rails_apps_pages', github: 'RailsApps/rails_apps_pages'
end
Use Bundler
Use Bundler to install the gems:
$ bundle install
Generate a Home Page
To run the generator and create a home page:
$ rails generate pages:home
The generator will create:
- app/views/visitors/index.html.erb
- app/controllers/visitors_controller.rb
- plus tests (if RSpec is installed)
It will add a route to the config/routes.rb file:
root :to => "visitors#index"
Why a “Visitors” controller? Why not a “Home” controller or “Welcome” controller? Those names are acceptable. But the home page often implements a user story for a persona named “visitor,” so a “Visitors” controller is appropriate.
Generate an “About” Page
To run the generator and create an “About” page:
$ rails generate pages:about
The generator will create:
- app/views/pages/about.html.erb
- plus tests (if RSpec is installed)
You’ll need to install the high_voltage gem for the “About” page. The high_voltage gem makes it easy to add pages with static content (text that doesn’t change), incorporating a site-wide application layout. The high_voltage gem provides the controller and routes needed to display any pages found in the app/views/pages/ folder.
Generate User Pages Requiring Authentication
If you have a User model and authentication with Devise or OmniAuth, you can add pages to display a list of users or each user’s profile, restricted to signed in users.
To run the generator and create pages to accompany a User model:
$ rails generate pages:users
The generator will create:
- app/controllers/users_controller.rb
- app/controllers/visitors_controller.rb
- app/views/users/_user.html.erb
- app/views/users/index.html.erb
- app/views/users/show.html.erb
- app/views/visitors/index.html.erb
It will also add routes to the config/routes.rb file.
Generate User Pages Requiring Authorization
If you have a User model, authentication with Devise, and authorization with Pundit, you can add pages to display a list of users, restricted to an administrator. Each user can see his or her own profile if logged in.
To run the generator and create pages to accompany a User model with Pundit authorization:
$ rails generate pages:authorized
All files are identical to the rails generate pages:users
generator, except:
- app/views/users/_user.html.erb
- app/controllers/users_controller.rb
- app/policies/user_policy.rb
- plus tests (if RSpec is installed)
Clean a Gemfile
Remove commented lines and multiple blank lines from a Gemfile:
$ rails generate clean:gemfile
Clean a Routes File
Remove commented lines and multiple blank lines from the file config/routes.rb:
$ rails generate clean:routes
Generate Analytics Files
Add a file containing JavaScript code to set up page-view tracking with Google Analytics:
$ rails generate analytics:google
Add a file containing JavaScript code to set up page-view tracking with Segment.io:
$ rails generate analytics:segmentio
Issues
Any issues? Please create an issue on GitHub. Reporting issues (and patching!) helps everyone.
Credits
Daniel Kehoe maintains this gem as part of the RailsApps project.
Please see the CHANGELOG for a list of contributors.
Is the gem useful to you? Follow the project on Twitter: @rails_apps. I’d love to know you were helped out by the gem.
MIT License
Copyright © 2014 Daniel Kehoe