1. Install basic rails app - agentbright/rails-guide GitHub Wiki

rails-guide

  • Create new application
  • Add postgres database
  • Install gems
  • Devise
  • Foundation
  • Active Admin
  • jquery-ui-rails
  • simple_form
  • jquery-turbolinks

Project - Create IT Capabilities Application

  1. Create git repo on github.com
  1. Clone to local
  1. Create new rails app
  • rails new it-capabilities
  1. Make sure it runs
  • bundle install
  • rails s
  1. Add postgresql
  • Remove the line gem 'sqlite3' from the gemfile
  • Add gem 'pg' to the gemfile
  • Add settings file
  • remove ‘development.sqllite3` file
  • Run rake db:create
  • Run rake db:migrate

Add the following gems

  • devise - https://github.com/plataformatec/devise
    • Install gem
    • run installer - rails generate devise:install
    • Run rails generate devise User
    • Run rake db:migrate
    • Install devise_security_extension - https://github.com/phatworx/devise_security_extension
      • Add gem
      • Run installer - rails generate devise_security_extension:install
  • simple_form
    • Add simple_form gem
    • DON’T RUN THE GENERATOR YET
    • Add country_select gem
  • Install Foundation
    • Add gem foundation-rails
    • Also add the rails_layout gem, but it should only be installed in the development environment. To do this, put it inside a :development group in the gemfile, like this:
    group :development do
      gem 'rails_layout'
    end
    
    • Run rails layout generator rails generate layout:install foundation5 --force
    • Run simple_form generator rails generate simple_form:install --foundation
  • jquery-ui-rails - https://github.com/joliss/jquery-ui-rails
    • add to application.js and application .css
  • jquery-turbolinks - https://github.com/kossnocorp/jquery.turbolinks
    • Install gem
    • add to application.js
  • active admin - http://activeadmin.info/
    • INstall active admin 1.0.0 gem
      • add gem 'activeadmin', github: 'gregbell/active_admin' to gemfile
    • Run active admin installer
      • rails generate active_admin:install
    • Run rake db:migrate and then start the server (rails s)
    • Go to http://0.0.0.0:3000/admin
    • Log in - Username: [email protected] Password: password
    • Register the User model with ActiveAdmin
      • run rails generate active_admin:resource User
  • Add settings.yml file to the config folder
  • Add setup.rake to lib/tasks

Configure Devise

  1. In config/initializers/devise.rb:
  • Change config.password_length = 8..128 to config.password_length = 6..128
  • Change # config.scoped_views = false to config.scoped_views = true
  1. In app/models.user.rb
  • Change :validatable to :secure_validatable
  1. Add a new field to the users tables called super_admin
  • Run rails g migration AddSuperAdminToUsers
  • Go to db/migrate and open up the very last file (it should have a really long name)
  • Inside the class, there should be this method to add a new column (field) to the table:
def change
  add_column :users, :super_admin, :boolean, default: false
end
  • Run rake db:migrate
  1. Add devise views (the sign in, sign out, password reset, etc pages)
  • Run rails generate devise:views

Configure and Customize Application

  1. Set time zone
  • In config/application.rb, uncomment config.time_zone and set it to 'Eastern Time (US & Canada)'
  1. Add fonts folder to asset pipeline
  • Create a new folder in app/assets called fonts
  • In config/application.rb, add config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
  1. Add a services directory
  • In app/ add a directory called services
  • In config/application.rb, add config.autoload_path = %W(#{config.root}/app/services)
  1. Add Handy
  • Add gem 'handy' to gemfile
  • Run bundle install

Add home page

Now we want to add a home page. Every page needs to have these three things:

  1. A route, so that the page has a URL, and the URL will point to the right controller and the right page
  2. A controller method, which is where define the data or the actions that you want to happen when you go to that URL
  3. A view, which has the HTML page that the user actually looks at

Adding our first controller

First, we need to create a controller where the controller method for this home page is going to go. Since its the home page, will call this new controller the home controller. We can do this by using a generator:

  • Run rails generate controller home
  • The generator created a bunch of stuff. Most importantly, it created a file called home_controller.rb, located in app/controllers, and a directory called home, located in the app/views. Now we have a controller to put our method, and a place to put our view file.

1. Adding the route (the root route)

We want to be able to go to 0.0.0.0:3000 and see our home page. We also want to be able to go 0.0.0.0:3000/index and see our home page. First, let's see where those places currently take us. Navigate to both of these pages and see what happens:

0.0.0.0:3000:

0.0.0.0.3000/index:

The first URL took us to the default root route, which is where the user goes when they navigate to the main site address.

The second URL gave us an error. Because we haven't specified a route yet for index, rails did not know where to take us. If you see an error that says **No route matches...", then you know the problem is a missing or incorrect route.

We can add routes in the file routes.rb, located in the /config directory. To add the root route:

  • Add root 'home#index'
    • This it telling rails that if someone navigates to the root URL, then render the index method in the home controller

To add a route for /index:

  • Add get 'index' => 'home#index'
    • This tells rails that if someone navigates to /index, it should render the index method in the home controller

2. Add the controller method

Now if we navigate to 0.0.0.0:3000 or 0.0.0.0:3000/index, we see a different error message:

If you see this, the route is working great! We have a route, but it's telling rails to find the index method in the home controller... and we don't have an index method in the home controller. So let's make one!

  1. Go to app/controllers/home_controller.rb
  2. Add an index method
def index
end

Rails is really smart. It will automatically look for a view file with the same name as the method in a matching directory. So since we have the index method in home_controller.rb, rails will look inside views/home for a file called index. If it finds it, it will display it.

3. Adding the view!

We're almost there! Now we just need to add a view file.

  1. In app/views/home, create a new file called index.html.erb. We add .erb to the end of the file, so that we can use Embedded Ruby inside the HTML file.

  2. Add <h1>Homepage</h1> to the index file.

  3. Navigate to 0.0.0.0:3000 and 0.0.0.0:3000/index to check if our page is showing up

⚠️ **GitHub.com Fallback** ⚠️