Other Pages

Expand All

create_a_rails_app.step

tip do
  console_with_message "From here on, this guide assumes you have Rails 7.0.x. To check your Rails version, type this in the terminal:", "rails -v"
  fuzzy_result "Rails 7.0{FUZZY}.x{/FUZZY}"
  message "If your computer reports a Rails version less than 7.0, ask a TA help get you back on track."
end

step "Change to your home directory" do
  insert 'switch_to_home_directory'
end

step "Create a railsbridge directory" do
  console "mkdir railsbridge"
  message "`mkdir` stands for make directory (folder)."
  message "We've made a folder called `railsbridge`."
end

step "Change to your new railsbridge directory" do
  console "cd railsbridge"
end

step "Create a new Rails app" do

  console "rails new test_app -d sqlite3"

  message "The command's output is voluminous, and will take some time to complete, with a long pause in the middle, after all the 'create...' statements ending in 'bundle install'.  When it fully completes, it will return you to your home prompt."

  console "cd test_app"
  console "rails server"

  tip "In OS X, you may need to let Ruby accept incoming network connections through your firewall.  Select 'allow' in the pop up."

  tip "In Windows, you may need to let Ruby and Rails communicate through your firewall.  Say yes to the pop up."

  tip "Shortcut: Just type 'rails s'" do
    message <<-MARKDOWN
      Throughout your Rails programming career you're going to type `rails server` a
      lot.  Simply typing `rails s` is the same as `rails server`.
    MARKDOWN
  end

  message <<-MARKDOWN
    The first command should produce no output.
    If `rails server` starts up with no errors, you're golden! It'll look something like this:
  MARKDOWN

  fuzzy_result <<-TEXT
    => Booting Puma
    => Rails 7.0.3 application starting in development
    => Run `bin/rails server --help` for more startup options
    Puma starting in single mode...
    * Puma version: 5.6.4 (ruby 3.0.3-p157) ("Birdie's Version")
    *  Min threads: 5
    *  Max threads: 5
    *  Environment: development
    *          PID: 65783
    * Listening on http://127.0.0.1:3000
    * Listening on http://[::1]:3000
    Use Ctrl-C to stop
  TEXT

  message "If it does, congratulations! You've successfully installed Ruby AND Rails and started your server."

  tip "If it doesn't work, ask a TA for help."
  message "* In your browser, go to <http://localhost:3000>"
  img src: "img/successful_rails7_install.png", alt: "Screenshot of the browser on localhost 3000 showing the rails intro page"
  message <<-MARKDOWN
    * Don't worry about the details version numbers. You should see ruby 3 and rails 7.
    * Back in the Terminal window where you ran <code>rails server</code>, type **Control-C** (don't type this into the console, but hold the Control and C keys at the same time) to kill(stop) the server. Windows will ask "Terminate batch job (Y/N)?".  Type "Y".
  MARKDOWN

  important "On Windows, sometimes Control-C doesn't work. In that case, look for the key called 'Break' or 'Pause' and press Control-Break, then answer Y at the prompt. If there is no Pause/Break key on your keyboard, you can run `ruby script/rails server` instead of `rails server` which should allow Control-C to stop the server."
end

step "Generate a database model" do
  tip "If your prompt doesn't already show that you are (still) in the test_app folder" do
    console "cd test_app"
  end

  console <<-BASH
    rails generate scaffold drink name:string temperature:integer
  BASH
  console <<-BASH
    rails db:migrate
  BASH
  console <<-BASH
    rails server
  BASH

  message <<-MARKDOWN
    **Note:** the above are three separate commands. Type each line into the terminal separately, not as one single command.

    Wait until your console shows that the Puma server has started (just like before).  Then, in the browser, visit <http://localhost:3000/drinks>

    1. Click on "New drink"
    2. Enter Cappuccino for the name
    3. Enter 135 for the temperature.
    4. Click on "Create Drink".

    (The window where you ran `rails server` will display debugging information as you do this.)

    You should see: ![Screenshot of the drink detail page](img/drink-successfully-created-rails7.png)

    In your terminal, Hold Control and hit C (or on Windows, Control-Break, Y) to stop the rails server.
  MARKDOWN
end

next_step "deploy_a_rails_app"